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

1 answer

  • answered 2019-08-16 06:48 AZ_

    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';