Toggle between multiple .env files like .env.development with node.js

node.jsExpressEnvironment VariablesDotenv

node.js Problem Overview


I want to use separate .env files for each mode (development, production, etc...). When working on my vue.js projects, I can use files like .env.development or .env.production to get different values for the same env key. (example: in .env.development: FOO=BAR and in .env.production: FOO=BAZ, in development mode process.env.FOO would be BAR, in production i'd be BAZ).

I'm working on an Express server and want to use these same kinds of .env files to store the port, db uri, user, pwd...

I know I can edit the scripts in package.json like this:

"scripts": {
    "start": "NODE_ENV=development PORT=80 node ./bin/www",
    "start-prod": "NODE_ENV=production PORT=81 node ./bin/www"
}

but this gets messy when using multiple variables.

I've tried using dotenv but it seems like you can only use the .env file. Not .env.development and .env.production.

Can I use the dotenv package or do I need another one? Or could I do this without any package at all?

node.js Solutions


Solution 1 - node.js

You can specify which .env file path to use via the path option with something like this:

require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` })

Solution 2 - node.js

I'm using the custom-env npm package to handle multiple .env files. Just put this at the top of your code:

require('custom-env').env();

and it will load environment variables from the file .env.X, where X is the value of you NODE_ENV environment variable. For example: .env.test or .env.production.

Here is a nice tutorial on how to use the package.

Solution 3 - node.js

I just wanted to add this for other people who are having issues. The above two answers are both slightly off. let me explain. The guy above had to use path as in

  • the first example it's missing a ./ the ./ indicates current directory to Node.js. Thats why in the first example if the env file was not in the root of the PC it would never find it. Using path is a nasty work around but the best way to do it is to simply add a ./ this says "hey computer look in my current directory for this file".
//this says hey computer look in my current directory for this file. 
 
require('dotenv').config({ path: `./.env.${process.env.NODE_ENV}` })


//I reccomend doing a console.log as well to make sure the names match*
console.log(`./.env.${process.env.NODE_ENV}`)

Solution 4 - node.js

From answers from above: This is the final result that worked for me.

.env.dev file in src dir.

import path from 'path';

dotenv.config({ path: path.join(__dirname, `./.env.${process.env.NODE_ENV}`)});

Solution 5 - node.js

The official doc of dotenv does not recommend having multiple .env files.

"Should I have multiple .env files? No. We strongly recommend against having a "main" .env file and an "environment" .env file like .env.test. Your config should vary between deploys, and you should not be sharing values between environments."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonasView Question on Stackoverflow
Solution 1 - node.jsMarlornView Answer on Stackoverflow
Solution 2 - node.jsMartin OmanderView Answer on Stackoverflow
Solution 3 - node.jsJoshView Answer on Stackoverflow
Solution 4 - node.jsMoshe YaminiView Answer on Stackoverflow
Solution 5 - node.jsrom5jpView Answer on Stackoverflow