Files
discordeno/website/docs/beginner/env.md
Skillz4Killz 59631f2fac format
2023-10-28 13:25:09 +00:00

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

  1. Create a new file named .env in the root directory of your project.
  2. 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

  1. In your TypeScript file, import the dotenv package:
import dotenv from 'dotenv'
  1. 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.