How to read environment variables in Node.js?
Hello everyone! I'm new to Node.js and trying to figure out environment variables. I have an application that needs to connect to a database. I store the password and username in environment variables to avoid exposing them in the code (especially if I push to GitHub). But I can't figure out how to read them.
I created a `.env` file in the project root:
```
DB_USER=myuser
DB_PASS=secret123
PORT=3000
```
In my `app.js` I'm trying to do this:
```javascript
const dbUser = process.env.DB_USER;
const dbPass = process.env.DB_PASS;
const port = process.env.PORT;
console.log(dbUser); // undefined
console.log(dbPass); // undefined
console.log(port); // undefined
```
But the console always prints `undefined`. I've heard that Node.js doesn't read `.env` files by itself. What do I need to do?