Passing environment variables in npm-scripts

后端 未结 3 1630
说谎
说谎 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:30

    this is how I did it, first you need to install two dev-dependencies

    https://www.npmjs.com/package/env-cmd this load your env var from your file

    https://www.npmjs.com/package/cross-env this use environment variable in script

    example scripts:

    "env-cmd ./.config/prod.env cross-env-shell \"docker volume create $DOCKER_VOLUME\""
    

    this load $DOCKER_VOLUME env var from prod.env

    update: starting from env-cmd version 10, you need specify -f flag if you want to use a custom env file path

    "env-cmd -f ./.config/prod.env cross-env-shell \"docker volume create $DOCKER_VOLUME\""
    

提交回复
热议问题