This commit is contained in:
Skillz4Killz
2023-10-28 13:25:09 +00:00
parent ea9fca102a
commit 59631f2fac
17 changed files with 74 additions and 70 deletions
+8 -11
View File
@@ -11,7 +11,6 @@ Before you can set up a `.env` file with TypeScript in Node.js, you need to inst
`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.
@@ -19,28 +18,26 @@ Before you can set up a `.env` file with TypeScript in Node.js, you need to inst
VAR_NAME=value
For example:
```js
DB_HOST=localhost
DB_USER=admin
DB_PASS=password123
DB_HOST = localhost
DB_USER = admin
DB_PASS = password123
```
## Step 3: Load Environment Variables
1. In your TypeScript file, import the `dotenv` package:
```ts
import dotenv from 'dotenv';
import dotenv from 'dotenv'
```
2. Call the config method of the dotenv package to load the environment variables from the .env file:
```ts
dotenv.config();
dotenv.config()
```
## Step 4: Access Environment Variables
@@ -48,9 +45,9 @@ dotenv.config();
You can now access your environment variables using the process.env object. For example:
```ts
const dbHost = process.env.DB_HOST;
const dbUser = process.env.DB_USER;
const dbPass = process.env.DB_PASS;
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.