Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
8
rated 0 times [  11] [ 3]  / answers: 1 / hits: 7996  / 3 Years ago, sun, may 23, 2021, 12:00:00

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.


enter


More From » node.js

 Answers
4

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.


[#1324] Monday, May 17, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mustafaericho

Total Points: 322
Total Questions: 103
Total Answers: 110

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mustafaericho questions
Mon, May 31, 21, 00:00, 3 Years ago
Sat, Feb 13, 21, 00:00, 3 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Tue, Apr 14, 20, 00:00, 4 Years ago
;