Why is Node not reading the env var of the current shell?

As you can see below, I am setting an environment variable FOO

, but when I execute console.log

on the next line, this undefined

. If I install it on the same line where I execute console.log

it is present. Why behavior as such?

$ FOO="123"
$ echo $FOO
123
$ node -e "console.log(process.env.FOO)"
undefined
$ FOO="123" node -e "console.log(process.env.FOO)"
123

      

Node version: 6.6.0

+3


source to share


1 answer


If you want to set an environment variable for the current terminal session, you need to use EXPORT

So this code will work



export FOO=123
node -e "console.log(process.env.FOO)"
123

      

EXPORT

makes the assignment visible to subprocesses. For details see.

+5


source







All Articles