Why is this if/else statement for setting a NODE_ENV returning undefined as opposed to the correct string?

时间:2016-12-02 05:06:15

标签: javascript node.js reactjs

I have a mongo.js file in my react application that I want to set an environment variable to the correct database. I do this dynamically with the following code at the top of my file:

let url = '';

if (process.env.NODE_ENV === 'development')
  url = process.env.REACT_APP_DATABASE;
else
  url = process.env.MONGODB_URI;

Wen I try to start the node server I get this error TypeError: Parameter "url" must be a string, not undefined Why is it evaluating to undefined as opposed to the correct string?

2 个答案:

答案 0 :(得分:0)

Either process.env.REACT_APP_DATABASE or process.env.MONGODB_URI is being assigned and is undefined. Have you examined the values in a debugger?

答案 1 :(得分:0)

问题是由于package.json文件中的疏忽造成的。看起来我没有安装dotenv。糟糕。