node environment variables code example
Example 1: how to read environment variable in node js
var myEnvVariable = process.env.ENV_VARIABLE_NAME
Example 2: node start with environment variables
NODE_ENV=production && OTHER_ENV=foo && node ./index.js
NODE_ENV=production && OTHER_ENV=foo && npm start
$env:NODE_ENV="production"; $env:OTHER_ENV="foo"; node .\index.js
$env:NODE_ENV="production"; $env:OTHER_ENV="foo"; npm run build
set NODE_ENV=production && set OTHER_ENV=foo && node .\index.js
set NODE_ENV=production && set OTHER_ENV=foo && npm run build
Example 3: windows how to set process.env variables
set NODE_ENV=production
Example 4: set a variable in express.js
app.set('oneSetting', 'one');
app.set('twoSetting', 'two');
app.set('view engine','jade');
console.log(app.settings.oneSetting);
console.log(app.settings.twoSetting);
console.log(app.settings['view engine']);
Example 5: node how to set environment variable
// get an environment variableexport const token = process.env['API_TOKEN'];// set an environment variable at runtimeprocess.env['RANDOM_ID'] = Math.random();