33

Is it possible to have a single .env file for all different deployment environments such as development, production , etc.Based on the environment the corresponding environment variables file needs to be loaded.

3
  • It is not clear, what you are trying to achieve. What is the importance of the .env format? Why can't you use a .json file? Commented Feb 4, 2018 at 7:26
  • Hi, env file is different from config file. Env file is hidden and has all configuration such as encrypted password ,file path , etc similar to windows environment variables. It will not be good practice to do this in config file. Commented Feb 4, 2018 at 7:28
  • 1
    That doesn't make any sense. A file doesn't become a config file just because you put the .json extension to that. You can have a file called env.json and include all passwords etc there. You don't need to include the file in your repository. Commented Feb 4, 2018 at 7:55

4 Answers 4

58

Yes. You can use the dotenv module for example:

.env

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

app.js

require('dotenv').config()

const db = require('db')
db.connect({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS
}
Sign up to request clarification or add additional context in comments.

Comments

12

Install the dotenv module

npm install dotenv 

.env

NODE_ENV=development
PORT=3000

index.js

let dotenv = require('dotenv').config()
console.log(dotenv);

Output:-

{ parsed: { NODE_ENV: 'development', PORT: '3000' } }

File strictures:-

---| index.js
   | .env

Comments

5

yes too late for answer to this question..

but have a simple and easy way for use env file in express or any other node apps and no need install external package.

node --env-file=.env app.js

Also, you can pass multiple env file arguments. Subsequent files override pre-existing variables defined in previous files.

node --env-file=.env --env-file=.development.env app.js
  • you need nodejs +20
  • for more detail visit original nodejs document from here

Comments

0

Yes, not necessarily .env file but a json/js file.

You can make a file like below and require this file with environment -

let config = require('./pathToFile/')[process.env.NODE_ENV]

Your file -

{
"development" : {
    "dbConfig" : {
        "username" : "acaca",
        "password" : "ajbcjdca",
        "port" : "acdc",
         "etc" : "etc"
    },
    "serverConfig" : {
      "host" : "jabcjac.com",
      "port" : "4545",
      "etc" : "etc"
    },
    "AWSConfig" : {
      "accessKey" : "akcakcbk",
      "etc" : "etc"
    }
},
"production" : {
    "dbConfig" : {
        "username" : "acaca",
        "password" : "ajbcjdca",
        "port" : "acdc",
         "etc" : "etc"
    },
    "serverConfig" : {
        "host" : "jabcjac.com",
        "port" : "4545",
        "etc" : "etc"
    },
    "AWSConfig" : {
        "accessKey" : "akcakcbk",
        "etc" : "etc"
    }
},
"test" : {
    "dbConfig" : {
      "username" : "acaca",
      "password" : "ajbcjdca",
      "port" : "acdc",
       "etc" : "etc"
    },
    "serverConfig" : {
      "host" : "jabcjac.com",
      "port" : "4545",
      "etc" : "etc"
    },
    "AWSConfig" : {
      "accessKey" : "akcakcbk",
      "etc" : "etc"
    }
}
}

2 Comments

Hi Rahul, Thanks. But the application requires .env file which has all the environment related configuration which is necessary for the application. The purpose of .env is it varies for each environment. We need to achieve this for .env so that deployment becomes easier and does not require to keep changing the content of file manually.
@goutham It's generally considered bad practice to use .env files in production. The production environment should always be set explicitly. An incorrectly configured web server can serve the .env file to the public. You also risk accidentally committing it to source control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.