32

I am on Ubuntu 12.04 and I'm just learning about environment variables. I am trying to read a custom variable from within my application but it always shows up as undefined. Here is the code of my test app:

// app.js

console.log('Value: ' + process.env.NODE_ENV);

If I run the following commands you will see that the variable has a value:

$ NODE_ENV=production
$ echo $NODE_ENV
production

I can echo $NODE_ENV all day and it will continue to show me "production", but when I do process.env.NODE_ENV in my Node application it always displays "undefined".

$ node app.js
Value: undefined

Here is the odd part though, if I display another environment variable that I know already exists, say process.env.PATH, then it works.

$ node app.js
Value: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Another quirk is that the command printenv list doesn't appear to contain my custom variable NODE_ENV despite the fact that echo $NODE_ENV shows me the correct value. printenv NODE_ENV shows nothing as well, but printenv PATH shows the proper value just as it did when I accessed PATH in my node application.

halfer
  • 19,824
  • 17
  • 99
  • 186
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • [Read environment variables in Node.js](https://stackoverflow.com/q/4870328/608639) – jww Oct 17 '18 at 07:48

6 Answers6

39

You need to export shell variables in order to make them available to processes you execute in your shell.

Compare the output of this command:

FOO=bar; bash -c 'echo $FOO'

with the output of this:

export FOO=bar; bash -c 'echo $FOO'
lanzz
  • 42,060
  • 10
  • 89
  • 98
  • 1
    The strange this is that even if I can see variable value using `echo $FOO` I can't see it using `process.env.FOO` anyway... – vagifm May 02 '21 at 19:59
14

I found my way here from something really silly.

I had just added the new exported variables, but my node process still wasn't seeing them. Then I realized it wasn't enough to restart the node process—I had to open a new terminal (ie. bash instance) too. Once I did this, it worked fine :)

MalcolmOcean
  • 2,807
  • 2
  • 29
  • 38
  • Thank you, it's working! I got really frustrated why it was 'undefined', all I had to do was exit bash instance and open a new one. – user2768479 Mar 05 '20 at 12:28
  • how do you do that? aren't these variables just set in the current shell instance? if you close the current shell and open a new one, wouldn't these variables be lost anyway? – gaurav5430 Jul 17 '22 at 12:31
  • In my case, I even had to restart Visual Studio Code. Killing and restarting the integrated terminal wasn't enough – Tobi Obeck Aug 28 '22 at 21:17
1

If you are using dotenv make sure to configure before you access the variables in your environment. The order of your code is mattered in js.

yasir hasan
  • 87
  • 1
  • 3
0

You might want to consider using a library for managing app configuration.

For example nconf helps manage configuration through

  • command line argumets
  • environment variables
  • files
  • etc..

And looking at the source is a nice way to learn https://github.com/flatiron/nconf

250R
  • 35,945
  • 7
  • 33
  • 25
0

Found my way here from something really silly as well ! I was running my server file from a "src" folder inside my main project directory and so had my .env inside the "src" folder itself (from where my server was running). Once I put the .env in my main project directory, it worked for me. Make sure that your '.env' is in the main directory and not inside "src" folder.

-2

Restart your bash (source ~/.bashrc). This will take into account your system environment.