17

I want to work with environment variables. Unfortunately, I am an inexperienced developer and decided very late to implement such a solution in my project.

I'm trying to inject environment variables, located in .env file, to all JS files (not all of them using environment variables, but I thought it would be faster and easier). Currently, I'm using dotenv package but it is obviously working in one file at once.

Should I use dotenv the standard way? Maybe there's a vulnerability I don't know of and that's why it is very unpopular to use environment variables this way.

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}
simplecreator
  • 393
  • 1
  • 2
  • 10
  • 3
    no, if you're using an entrypoint file as standard. load it early on then required files will see your process.env.* vars just fine. though you shouldn't put it in any condition, else your chicken/egg if .env contains NODE_ENV and forced to set it early – Lawrence Cherone Nov 03 '19 at 21:10

4 Answers4

30

You don't need to write require('dotenv').config() in every file. Just include this as the top statement in the index.js or the main file that got executed at the very first place when you run your program.

amit gupta
  • 808
  • 5
  • 8
  • Could you give an example? What to you call *top statement* ? – Sandburg Jul 11 '22 at 10:27
  • I don't think this works. Other files imported in the main file will be imported before `dotenv.config()` finishes adding to `process.env` even if that's that first line in the main file. – jordanfb May 09 '23 at 21:30
17

Like the comment on your file mentioned, you should have an entry point for your ENVs. You don't want to require('dotenv') in every file.

Instead, create a new file (named something like environment.js) that is in the utils or core folder.

require('dotenv').config();

/* eslint no-process-env:0 */
module.exports.default = {

    env: process.env.env,
    url: process.env.url,
    apiUrl: process.env.apiUrl,
    logLevel: process.env.logLevel,

    db: {
        host: process.env.db_host
        port: process.env.db_port
    }
    // Grab everything in you .env file here
}

Then in each of your other files you can include your configs in a nice json object.

const config = require('../utils/environment');

dbConnector(config.db.host, config.db.port);
// blah blah blah
MindlessRouse
  • 425
  • 2
  • 12
6

You accepted the first comment but it's not the right way/answer. you can include require('dotenv').config() at the very beginning of your entry file (index.js , server.js ..) and you'll get the process.env variables anywhere in your app by just calling process.env.VARIABLE_NAME

Hakim Bencella
  • 187
  • 4
  • 12
  • 1
    does not work for typescript – Will Zap Dec 13 '22 at 03:41
  • Doesn't work. Other project files, which the OP is looking for, will not have the env vars. – jordanfb May 09 '23 at 21:31
  • This works. but you should be careful not to import or require any file before require dotenv config. otherwise process.env only works in your entry point file of the app. that means index.js or server.js. if you have required dotenv and have called to config file in a separate line be careful to do it before importing or requiring any other file. – Sapthaka May 21 '23 at 12:06
0

In below example, the file /config/db.config.js using some env variable so I need to add the dotenv.config() line before importing the file.

this is index.js file

const express = require('express');
require('dotenv').config();
const dbConfig = require('./config/db.config');

The conclusion is add dotenv config line on the top of the index.js