Anubhav Dhawan
Anubhav Dhawan

Reputation: 1487

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?

Upvotes: 10

Views: 21892

Answers (2)

Kerem atam
Kerem atam

Reputation: 2797

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\""
}

Upvotes: 14

potato
potato

Reputation: 471

Will using $SERVER work for you?

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

Upvotes: 4

Related Questions