How to customize Node.js .env files for different environment stages

Erisan Olasheni
Oct 5, 2018 · 3 min read

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: process.env[ENV_VARIABLE]=VALUE).

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 process.env object.


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.

APP_NAME=custom environment app
// Require custom-env and set your preferred env file
require ('custom-env').env('staging')

Expected Output

custom environment app

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 env() method.


File: index.js

// Pass true to env() to make it use the current environment stage. require('custom-env').env(true)console.log(process.env.APP_NAME)

Now let’s define a staging configuration file:

File: .env.staging

APP_NAME=Staging Node App

Now let’s serve node with the staging environment:

NODE_ENV=staging node index.js

Expected Output

Gets the variables according to the staging environment.

There you go!

Full Documentation

For the full documentation of custom-env, visit the npm page

Source Code

You can get or contribute to the custom-env source code at

Happy Coding!

This is no longer updated. Go to instead

Erisan Olasheni

Written by

Follower of Christ, A web and software developer, open sorcerer with proficiency in PHP/Laravel, Python/Django, JavaScript/NodeJS — Lover of Music and Tech

This is no longer updated. Go to instead

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade