Even though I can see that an environment variable was created on windows, process.env
always returns undefined
. I did set all my variables, and when I check them manually, they all appear in the prompt, but the process.env
always stays undefined.
P.S. I don't have admin privileges, except when I check the process.env.NODE_ENV
.
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.