How to start node app with development flag?

前端 未结 5 562
梦如初夏
梦如初夏 2021-02-07 05:41

At top of my app.js file I put

NODE_ENV=\'development\';

but I get error that NODE_ENV is not defined. But in the nodejs documentation is says

相关标签:
5条回答
  • 2021-02-07 06:00

    NODE_ENV is an environment variable.
    You set it in your shell when you invoke Node.js.

    However, development is the default; you only need to do anything if you want prod.

    0 讨论(0)
  • 2021-02-07 06:11

    It is better to start your app in dev mode like this:

    NODE_ENV=development node app.js
    

    But if you really wanted to set it your app file just set it like this:

    process.env.NODE_ENV= "development"
    
    0 讨论(0)
  • 2021-02-07 06:19

    If you want to set an environment variable in your js file you should do it this way:

    process.env.NODE_ENV = 'development';
    

    Alternatively you can set the variable in your shell and run your application:

    $ NODE_ENV="development" node ./app.js
    

    or export the variable and run your application:

    $ export NODE_ENV="development"
    $ node ./app.js
    

    On Windows:

    $ set NODE_ENV="development"
    $ node app.js
    
    0 讨论(0)
  • 2021-02-07 06:20

    By using NPM you might to be used the follow scripts in the package.json:

      "scripts": {
        "start": "nodemon ./bin/www",
        "dev_win": "set NODE_ENV=development && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log",
        "prod_win": "set NODE_ENV=production && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log"
        "prod_nix": "NODE_ENV=production node ./bin/www >> /romba/log/api.log 2>> /romba/log/_error.log"
      },...
    

    To start one of the script use the command:

     npm run-script prod_win
    

    In the JavaScript code I check the condition:

    process.env.NODE_ENV.indexOf('production') > -1
    
    0 讨论(0)
  • 2021-02-07 06:20

    In order to start from cmd you can try

    NODE_ENV=development node yourappname.js
    

    If you are doing this in server where forever is installed you can mention the environment variable like

    NODE_ENV=development forever start yourappname.js
    
    0 讨论(0)
提交回复
热议问题