Passing environment variables in npm-scripts

后端 未结 3 1637
说谎
说谎 2021-02-03 17:30

I have a package.json with following (simplified) content in the scripts key:

...
scripts: {
   \"start\": \"NODE_ENV=${NODE_ENV:=production} node start-app.js\"         


        
3条回答
  •  星月不相逢
    2021-02-03 18:17

    You have a few options:

    • better-npm-run,which can define an env for each command separately
    • Instead of a poststart script, you can concatenate commands for npm like so: "start": "NODE_ENV=${NODE_ENV:=production} node start-app.js && echo $NODE_ENV"
    • Use a process manager in production like pm2. pm2 lets you define environment specific json files with settings such as NODE_ENV. At our company, we successfully run all of our apps in different environments with pm2 (all the while having the same start command)

提交回复
热议问题