mirror of
https://github.com/discordeno/discordeno.git
synced 2026-05-30 15:30:07 +00:00
1.2 KiB
1.2 KiB
sidebar_position, sidebar_label
| sidebar_position | sidebar_label |
|---|---|
| 4 | Setup .env |
How to Set Up a .env File with TypeScript in Node.js
Step 1: Install Required Packages
Before you can set up a .env file with TypeScript in Node.js, you need to install the required packages. Run the following command in your project's directory:
npm install dotenv @types/dotenv --save-dev
Step 2: Create a .env File
- Create a new file named
.envin the root directory of your project. - Add your environment variables to the file in the following format:
VAR_NAME=value
For example:
DB_HOST=localhost
DB_USER=admin
DB_PASS=password123
Step 3: Load Environment Variables
- In your TypeScript file, import the
dotenvpackage:
import dotenv from 'dotenv';
- Call the config method of the dotenv package to load the environment variables from the .env file:
dotenv.config();
Step 4: Access Environment Variables
You can now access your environment variables using the process.env object. For example:
const dbHost = process.env.DB_HOST;
const dbUser = process.env.DB_USER;
const dbPass = process.env.DB_PASS;
And that's it! You have now set up a .env file with TypeScript in Node.js and can access your environment variables in your code.