Node process.env.VARIABLE_NAME returning undefined

纵饮孤独 提交于 2020-05-11 03:48:27

问题


I'm using environment variables on my mac to store some sensitive credentials, and trying to access them through Node. I added them into my environment profile with

export VARIABLE_NAME=mySensitiveInfo

When I use echo $VARIABLE_NAME I receive the correct output (my sensitive info).

However, when I am trying to access this same variable in Node with process.env.VARIABLE_NAME and try to print it out on the console, I get an undefined.

Other environment variables appear to be okay though. For example, when I console.log(process.env.FACEBOOK_CALLBACK_URL), it prints the correct value to my console. I added FACEBOOK_CALLBACK_URL a few days ago.

Do I have to restart my machine or something? Does it take a certain time before environment variables become available in Node? The closest answer I've seen on SO is this post, but nobody was able to figure out why it was happening.


回答1:


process.env.VARIABLE_NAME returns undefined because the Node.js execution environment does not know the newly added VARIABLE_NAME yet. To fix the issue, the Node.js execution environment (e.g. IDE) need to restart.

The following steps can be used to reproduce this issue:

  1. Open IDE such as WebStorm and write a simple Node.js program: console.log(process.env.VARIABLE_NAME). It will print undefined as expected, as VARIABLE_NAME is not defined yet. Keep the IDE running, don't close it.
  2. Open environment profile such as .bash_profile and add export VARIABLE_NAME=mySensitiveInfo in it.
  3. Open system console and run source .bash_profile, so that the above export statement will be executed. From now on, whenever system console is opened, VARIABLE_NAME environment variable exists.
  4. In system console, execute the Node.js program in step 1, it will print mySensitiveInfo.
  5. Switch to IDE and execute Node.js program, it will print undefined.
  6. Restart the IDE and execute Node.js program, this time, it will print mySensitiveInfo



回答2:


I got a problem just now , and I use this solved it in webpack config

const plugins = [
    new webpack.NoEmitOnErrorsPlugin(),
    // after compile global will defined `process.env` this Object
    new webpack.DefinePlugin({
      BUILD_AT : Date.now().toString(32),
      DEBUG: process.env.NODE_ENV !== 'production',
          'process.env': {
              'NODE_ENV': JSON.stringify(process.env.NODE_ENV || "development"),
              'VARIABLE_NAME': JSON.stringify(process.env.VARIABLE_NAME)
     }
   })
]



回答3:


nodemon.json file is only for setting nodemon specific configuration So for create custom environment variables we can use dotenv package

First , Install dotenv package

npm install dotenv --save

after that create .env file in root and include environment variables as bellows

MONGO_ATLAS_PW=xxxxx
JWT_KEY=secret

Finally, inside your app.js file insert following after your imports.

require('dotenv').config()

Then you can use environment varibale like this

process.env.MONGO_ATLAS_PW
process.env.JWT_KEY


来源:https://stackoverflow.com/questions/44915758/node-process-env-variable-name-returning-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!