最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node js, process.env not reading enviroment variables - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited May 23, 2021 at 18:56 Toni 1,5855 gold badges20 silver badges26 bronze badges asked May 23, 2021 at 15:48 Dardan GjokaDardan Gjoka 1031 gold badge1 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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 bee environment variables. You need to write them in the console and then they bee environment variables. The .env file method lets you write them to a file and read them from there through the donenv package.

发布评论

评论列表(0)

  1. 暂无评论