process.env.NODE_ENV is undefined

后端 未结 13 1698
南旧
南旧 2020-11-28 18:26

I\'m trying to follow a tutorial on NodeJs. I don\'t think I missed anything but whenever I call the process.env.NODE_ENV the only value I get back is undefine

相关标签:
13条回答
  • 2020-11-28 18:45

    You can also set it by code, for example:

    process.env.NODE_ENV = 'test';

    0 讨论(0)
  • 2020-11-28 18:47

    For people using *nix (Linux, OS X, etc.), there's no reason to do it via a second export command, you can chain it as part of the invoking command:

    NODE_ENV=development node server.js
    

    Easier, no? :)

    0 讨论(0)
  • 2020-11-28 18:49

    In UBUNTU use:

    $ export NODE_ENV=test

    0 讨论(0)
  • 2020-11-28 18:50

    If you faced this probem in React, you need react-scripts@0.2.3 and higher. Also for other environment variables than NODE_ENV to work in React, they need to be prefixed with REACT_APP_.

    0 讨论(0)
  • 2020-11-28 18:51

    We ran into this problem when working with node on Windows.

    Rather than requiring anyone who attempts to run the app to set these variables, we provided a fallback within the application.

    var environment = process.env.NODE_ENV || 'development';
    

    In a production environment, we would define it per the usual methods (SET/export).

    0 讨论(0)
  • 2020-11-28 18:57

    For me, the issue was that I was using the pkg library to turn my app into an executable binary. In that case, the accepted solutions didn't work. However, using the following code solved my problem:

    const NODE_ENV = (<any>process).pkg ? 'production' : process.env.NODE_ENV;
    

    I found this solution here on GitHub.

    0 讨论(0)
提交回复
热议问题