Configuring Environment Variables in Node.js. Hey guys, recently one of you asked a question in the MERN playlist. It was about managing environment variables, like MongoDB credentials, in a Node.js app. In the video, I extract the variables from process.env and assign easy-to-guess defaults as a fallback. The plan was to then provision a local MongoDB Docker container with those defaults, and use it for development. This way, I'd feel comfortable committing those variables to version control, since they were never intended to be used in production.
When it's time to deploy the app, I would overwrite the defaults with the actual secrets, and rely on the service provider to store them securely. In the end, I decided to use mLab, instead of Docker, which meant that I had to now leverage config variables in a dev environment as well. Since I'd not want them exposed in source code, hard-coding those vars in config.js was no longer an option. I did some digging and thought I'd share how I got around it in this video.
00:55 Linux / UNIX (e.g. MacOS) syntax
01:20 Windows CMD syntax
02:18 cross-env package
04:18 NPM script in package.json
05:44 .env file with secrets
07:06 eval command
08:25 export command
10:09 .gitignore file
11:14 dotenv package
12:14 node-env-run package
Windows users, beware of trailing spaces https://github.com/remy/nodemon/issues/184#issuecomment-87378478
export / eval commands https://stackoverflow.com/questions/19331497/set-environment-variables-from-file
Working with Environment Variables in Node.js https://www.twilio.com/blog/2017/08/working-with-environment-variables-in-node-js.html