You need to read them first.
Use the dotenv package.
Install:
npm install dotenv
In you project code:
require('dotenv').config()
Add .env file in you project folder like this:
DB_HOST=localhost
DB_USER=root
DB_PASS=*be sure there is strong pass*
Try get env var like this:
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
By the way, it is not enough to write them to a file in order for them to become environment variables. You need to write them in the console and then they become environment variables. The .env file method lets you write them to a file and read them from there through the donenv package.