简体   繁体   中英

How to get environment variable in npm script?

I am trying to access an enviroment variable in the npm script itself like so:

"scripts": {
  "test": "istanbul cover node_modules/.bin/_mocha --root ../SERVER/routes -- --recursive"
},

And start this script like so:

SERVER=somewhere npm test

How can I get the resolved value of SERVER variable in the npm script in the package.json itself?

For the windows users, you may use your variables like this: %SERVER% instead of $SERVER .

Or better approach to use cross-env module which will allow you to do it like linux on all platforms:

npm i cross-env

And use it :

"scripts": {
  "test": "cross-env-shell \"istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive\""
}

使用$SERVER对你$SERVER吗?

"scripts": { "test": "istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive" }

It would be better to use env and get the value inside the script with process.env instead. It's a widely used practice.

package.json is just a json file, it even doesn't allow comments, so I'd not suggest to use this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM