How to start node app with development flag?

前端 未结 5 559
梦如初夏
梦如初夏 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: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
    

提交回复
热议问题