How can I set an environmental variable in node.js?
You can set your environment variables in process.env
:
process.env['VARIABLE'] = 'value';
-OR-
process.env.VARIABLE = 'value';
Node should take care of the platform specifics.
First you should install this package :-
https://github.com/motdotla/dotenv [npm install dotenv
]
Then you need to create a .env file in your project's root directory, and there you can add variables like below:-
NODE_ENV=PRODUCTION
DATABASE_HOST=localhost
Now you can easily access these variables in your code like below:-
require('dotenv').config()
console.log(process.env.NODE_ENV);
It worked for me, hopefully that helps.