by Erisan Olasheni
How to customize Node.js .env files for different environment stages
Have you ever found yourself in a situation where you needed custom environment variables for different development stages of your app? Here is a one-line solution.
Development has been much easier since the invention of the
.env file. You can easily set your environment variables and values with the syntax
ENV_VARIABLE=VALUE and boom! These variables got loaded as your environment variables, making it possible to get quick access to them:
In case you are still wondering what all this means, well, you are probably new to the
.env file. It’s actually a simple configuration text file that is used to define some variables you want to pass into your application’s environment.
This file needs a something like a parser to make it work. The parser reads the variable definitions one-by-one and parses them to the environment. It uses the format ENV_VARIABLE=VALUE (in the case of Node.js:
Of course, this is not a built-in feature in Node.js. You have to engineer it with a popular module called dotenv.
It’s a nice workaround, as it has really made development easier between co-developers and across the dev community as a whole. I personally had been using the dotenv module, until I got stranded trying to get a solution that could make me use a different configuration file for a particular environment. That would be even cooler…right? Yes! But unfortunately, the dotenv module doesn’t provide us with this goody.
So what’s next? We need this thing to make development and testing easier across different development stages!
How about custom .env files for different environment stages?
Don’t you think that would be a good solution? Defining custom environment variables by just creating a .env.envname file? Cool! That is what custom-env has come to do.
Custom env is a library built to make development easier by allowing multiple .env configuration for different environments. This is done by loading environment variables from a .env.envname file into the node’s
Just grab it with the following command:
npm i custom-env
By default, custom-env picks the .env file for your dev stage. However, to customize for a different stage, add the name as a suffix as in .env.envname.
We can define a custom environment variable for a staging development.
- Create a .env.staging file
- Define your variables
APP_ENV=staging APP_NAME=custom environment app DB_HOST=localhost DB_USER=user DB_PASS=pass
- Access your variables
// Require custom-env and set your preferred env file require ('custom-env').env('staging') console.log(process.env.APP_ENV) console.log(process.env.APP_NAME) console.log(process.env.DB_HOST) console.log(process.env.DB_PASS)
staging custom environment app localhost user pass
That’s it, pretty easy. Feel free to define more variables for different stages you think you have, like:
.env.testing, .env.staging, .env.server1, .env.server2, .env.localhost
Set to the Current Environment
You can tell custom-env to use a configuration that matches your current development stage by passing true to the
// Pass true to env() to make it use the current environment stage. require('custom-env').env(true) console.log(process.env.APP_NAME) console.log(process.env.USERNAME) console.log(process.env.PASSKEY)
Now let’s define a staging configuration file:
APP_NAME=Staging Node App USER_NAME=John PASSKEY=J*h*
Now let’s serve node with the staging environment:
NODE_ENV=staging node index.js
There you go!
For the full documentation of custom-env, visit the npm page https://www.npmjs.com/package/custom-env
You can get or contribute to the custom-env source code at https://github.com/erisanolasheni/custom-env