1455

Is there a way to read environment variables in Node.js code?

Like for example Python's os.environ['HOME'].

Phillip
  • 5,017
  • 9
  • 40
  • 61
Jayesh
  • 49,175
  • 21
  • 70
  • 98

8 Answers8

2064
process.env.ENV_VARIABLE

Where ENV_VARIABLE is the name of the variable you wish to access.

See Node.js docs for process.env.

Michał Perłakowski
  • 80,501
  • 25
  • 149
  • 167
Jayesh
  • 49,175
  • 21
  • 70
  • 98
  • 5
    Note that this will not be visible outside the node process and its subprocesses. E.g. it wouldn't be visible if you fire `env` in another shell window while the node process is running, nor in the same shell after the node process exits. – Marko Bonaci May 30 '15 at 10:10
  • 24
    this also works for assigning variables. `process.env.FOO = "foo";` works. – chicks Sep 11 '15 at 17:16
  • 30
    It's worth mentioning that this does not work in a React application. `process.env` is sanitized for security reasons. Only variables that begin with `REACT_ENV_` are available. See: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-custom-environment-variables – Mark Edington Jul 11 '17 at 12:25
  • 11
    @MarkEdington I think it should be `REACT_APP_` – Mr. 14 Oct 19 '17 at 15:35
  • 8
    @Mr.14 Right you are! It's REACT_APP_ not REACT_ENV_ – Mark Edington Oct 20 '17 at 18:17
  • 1
    Also for everyone using Vue, your env vars need to be prefixed with `VUE_APP_` – villy393 Aug 04 '18 at 20:21
  • The URL describing React applications has changed - https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables – Peter Hull Jul 22 '19 at 11:02
  • Can you use `process.env` to modify the environment as well like you can in Python? – Alper Dec 08 '21 at 12:56
  • @Alper yes, but the modifications will not be reflected outside the node process – Fadi Mar 05 '22 at 12:11
160

When using Node.js, you can retrieve environment variables by key from the process.env object:

for example

var mode   = process.env.NODE_ENV;
var apiKey = process.env.apiKey; // '42348901293989849243'

Here is the answer that will explain setting environment variables in node.js

Community
  • 1
  • 1
Subodh Ghulaxe
  • 17,943
  • 14
  • 81
  • 99
  • 1
    what lib is required to use the above process.env method? – user_mda Nov 01 '15 at 19:27
  • 5
    @user_mda `process.env` is built into the node.js api. – Jason Axelson Jan 20 '16 at 01:47
  • Do I just set whatever I want on the process.env? why do people set it there as opposed to say, a config object that is require()'ed by node.js? – PDN Apr 27 '16 at 01:05
  • 4
    process.env gives you access to environment variable set at an operating system level. These can be set in various ways and will depend on where you are deploying your app For example, I often run my local app using NODE_ENV=development NODE_PATH=lib node server.js. Then process.env.NODE_PATH will return 'lib' – Russell Ormes May 06 '16 at 19:22
66

If you want to use a string key generated in your Node.js program, say, var v = 'HOME', you can use process.env[v].

Otherwise, process.env.VARNAME has to be hardcoded in your program.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
user2574650
  • 701
  • 5
  • 2
60

To retrieve environment variables in Node.JS you can use process.env.VARIABLE_NAME, but don't forget that assigning a property on process.env will implicitly convert the value to a string.

Avoid Boolean Logic

Even if your .env file defines a variable like SHOULD_SEND=false or SHOULD_SEND=0, the values will be converted to strings (“false” and “0” respectively) and not interpreted as booleans.

if (process.env.SHOULD_SEND) {
 mailer.send();
} else {
  console.log("this won't be reached with values like false and 0");
}

Instead, you should make explicit checks. I’ve found depending on the environment name goes a long way.

 db.connect({
  debug: process.env.NODE_ENV === 'development'
 });
Igor Litvinovich
  • 2,140
  • 12
  • 22
  • 3
    I like to use 'yes' and 'no' for boolean env vars which must be explicitly checked. This avoids problems in many programming languages. – Dogweather Aug 24 '19 at 04:23
42

You can use env package to manage your environment variables per project:

  • Create a .env file under the project directory and put all of your variables there.
  • Add this line in the top of your application entry file:
    require('dotenv').config();

Done. Now you can access your environment variables with process.env.ENV_NAME.

Huy Vo
  • 2,318
  • 6
  • 26
  • 43
6

If you want to see all the Enviroment Variables on execution time just write in some nodejs file like server.js:

console.log(process.env);

Juanma Menendez
  • 12,842
  • 6
  • 48
  • 46
4

Using process.env. If Home is your env variable name then Try this:

const HOME = process.env.HOME;

Or

const { HOME } = process.env;
Sahil Thummar
  • 904
  • 5
  • 8
-3

Why not use them in the Users directory in the .bash_profile file, so you don't have to push any files with your variables to production?

John Doe
  • 370
  • 3
  • 20
Brad Vanderbush
  • 167
  • 1
  • 11
  • 1
    The reason is because if you use `.bash_profile` then it will set it for that user's environment but if you are running multiple instances there then you have to set multiple env variables for that rather than having single one. E.g. if you set PORT variable then you have to make it like PORT_1, ... but if you use it through .env then you can use same code with .env file having different PORT number. – Raheel Shahzad Jul 30 '20 at 06:44