Skip to content

Commit 5f8289b

Browse files
authored
Update Node-and-Packages.md
1 parent 8f6d614 commit 5f8289b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Node-and-Packages.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,36 @@ Using the run script
151151

152152
```npm start```
153153

154+
## 9. `dotenv` Package
155+
156+
`dotenv` allows for environment variables configuration, such as managing API keys and credentials.
157+
158+
Install `dotenv`
159+
160+
```npm install dotenv```
161+
162+
163+
Edit the web server JavaScript file
164+
165+
```javascript
166+
require('dotenv').config();
167+
168+
const express = require('express');
169+
const app = express();
170+
const port = process.env.PORT;
171+
172+
console.log(port); // 5000
173+
```
174+
175+
Create a `.env` file
176+
177+
```
178+
PORT=5000
179+
```
180+
181+
Re-run the web server and all should still work as before, only this time the the port number is not exposed in the JavaScript code.
182+
183+
> Don't forget to add your `env` file to the `.gitignore`
184+
185+
[Read more about the dotenv Package](https://www.npmjs.com/package/dotenv)
186+

0 commit comments

Comments
 (0)