

Both files are exported by a third file i.e keys.js which checks the Node.js execution environement i.e. Using them can be tricky (when i initially got frustrated), So pay attention. Each file stores different variables for different environment. Instead of using package.json, we can use for development and for production environment. This approach is completely different from first approach and addresses some of the issue of first approach. Separating variables for development and production environment can be very hard.Don't put more than 2 variables as it could quickly becomes a mess.

Shouldn't not put any sensitive data in here as it is visible to everyone and you can't ignore package.json in the.Some points to be considered when using this approach: I also use this approach when I work with debug module or setting the Node.js execution environemnt. NOTE: This will probably won't work with Windows OS.Įnter fullscreen mode Exit fullscreen mode This is a valid a place to put environment variable but not a secure place.īelow is an example of setting Node.js execution environment to production on the npm's start script. You are thinking how? But you can pass key=value pairs directly in the npm scripts in the package.json. If you want to learn about environment variables in Node.js in click here. This post will walk you through the different approaches of using environment variables in Node.js. Working with environment variable in Node.js is very easy and simple.

Any mistake can lead your server to crash.

You have to make sure that your server is properly configured to make use of correct variables for both development and production environments. They always contains very sensitive data and doesn't meant to be shared with outside world. Hope y'all like it 🤞.Įnvironment variables are very fundamental part of developing with Node.js or any server side language.
