Problem

While writing NodeJS application i find it very difficult to manage different environment variables. For reading environment variable was using dotenv as rest of the developers. My approach was to create only one .env file and write down all variable files in it. So the problem was here i was mentioning everything in one file which was really difficult to manage.

NODE_ENV=development
PORT=3000
# Database
DB_HOST=
DB_USER=
DB_PASS=
DB_NAME=

Then i started creating different files based on the environment, like for development my file name will be .env.development and same for other environments and i started copying .env.<ENV>file to .env file based on the environment. Suppose i am working on development environment then i will copy .env.development file to .env and change variable values and start working. Same for the rest of the environment. But here still manual intervention was required. Suppose someone forget to change/copy variable values then you may get different content/application might break.

Solution of the problem

I found one very good package, env-cmd. It is a simple node program for executing commands using an environment from an env file.

How to Install env-cmd?

You can run below command to install env-cmd in your project. Here i am installing env-cmd as a devDependencies

npm install env-cmd --save-dev

To use this package what you have to do is, create environment files for each environment. Like for development environment you can create .env.development or for production environment you can create .env.production etc.

Here i am creating one env file for development. Filename .env.development

NODE_ENV=development
PORT=3000
DB_HOST=
DB_USER=
DB_PASS=
DB_NAME=

How to add env-cmd to package.json command?

Now you can pass env file names in commands you have created in package.json as follows

{
"name": "env-tuts",
"version": "1.0.1-RC1",
"description": "Env Tuts",
"private": true,
"scripts": {
"development": "npx env-cmd -f .env.development node index.js",
"production": "npx env-cmd -f .env.production node index.js",
"uat": "npx env-cmd -f .env.uat node index.js"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
},
"devDependencies": {
"env-cmd": "^10.1.0"
},
"license": "Apache v2.0"
}

Here in the scrips section you can see i have added npx env-cmd <env-file-name> node index.js this command will help us fetch environment variable based on the environments.

Please note, you can replace node index.js with any other commands depending on your need. Like if you want to run your application with nodemon just replace node index.js with nodemon index.js.

Test your application

Now once you are done with the above steps mentioned then you can try running your application. Here i am giving example of running application for development mode

npm run development

Conclusion

In this blog we have seen how to integrate different env files in your application depending on the environment. If you know any better approach feel free to comment. If you like this article please share. Thanks for reading. Happy Coding.