简体   繁体   中英

Setting up environment variable in nodeJs

How can we set environment variables in NodeJs

I have tried this in my terminal

NODE_ENV=prod
node index.js

and then inside my code when I try to log it ( config.js file which is imported in index.js and where i configure everything)

console.log(process.env.NODE_ENV)

it gives me undefined

Use

export NODE_ENV=prod

also you should create a .env file and keep all your environments in the same.

In your config file set the default values

NODE_ENV = process.env.NODE_ENV || 'prod';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM