how to set env variable and use them in node app
npm i config
custom-environment-variables.json
//for secret key keep the value empty string
{
"jwtPrivateKey":""
}
{
"dbHost": "localhost",
"dbPort": 5984,
"dbName": "node-sequelize"
}
// Now we have to assign value from a env vairable
{
"CONFIG_KEY":"ENVIRONMENT_VARIABLE_KEY"
}
ex:
{
"jwtPrivateKey":"mti_jwtPrivateKey"
}
windows
set mti_jwtPrivateKey=VALUE_OF_THAT_VARIABLE
Unix:
export mti_jwtPrivateKey=VALUE_OF_THAT_VARIABLE
echo $ENVIRONMENT_VARIABLE_NAME
ex:
echo $mti_SecretKey
process.env.PORT
const config = require('config');
config.get('jwtPrivateKey')
const config = require('config');
if (!config.get('jwtPrivateKey')) {
console.error('FETAL ERROR: jwtPrivateKey is not defined.');
process.exit(1);
}