1st way
npm install dotenv --save
- mkdir env
- config.env
NODE_ENV=your-env
PORT=your-port
DATABASE=your-db-Str
CLOUD_NAME=your-cloud-name
API_KEY=your-api-key
API_SECRET=your-api-secret
- app.js
let dotEnv = require('dotenv');
dotEnv.config({ path: './config.env' });
console.log(process.env.yourVariableName);
// Example
console.log(process.env.PORT);
2nd way
npm install properties-reader --save
- mkdir env
process.argv.forEach(function (val, index, array) {
let arg = val.split("=");
if (arg.length > 0) {
if (arg[0] === 'env') {
let env = require('./env/' + arg[1] + '.properties');
module.exports = env;
}
}
});
# Dev properties
[main]
# Application port to run the node server
app.port=8080
[database]
# Database connection to mysql
mysql.host=localhost
mysql.port=2500
let enviorment = require('./environments');
let PropertiesReader = require('properties-reader');
let properties = new PropertiesReader(enviorment);
let someVal = properties.get('main.app.port');
npm start env=development
// or
npm start env=production