I have been trying to get a streamline way of having different environment variables for local and production web apps, but I haven't come across the "ideal" solution yet.
There's the option of having a config.js file like so:
You could have own config file for each environment:
- environments - index.js - deveplopment.json - staging.json - production.json
To use appropriate config file, run the app with required
NODE_ENV=production node index
environments/index.js determinate the current
NODE_ENV and use config:
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; module.exports = require('./' + process.env.NODE_ENV);
If config file doesn't include secret info (apiKeys, etc), it can be pushed to repo. Otherwise add it to .gitignore and use environment variables on the server.
For advanced configuration use such packages as nconf.
It allows to create hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.