Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 767
Anubhav Dhawan Avatar asked Oct 13 '16 13:10

Anubhav Dhawan


1 Answers

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\""
}
like image 102
Kerem atam Avatar answered Oct 08 '22 23:10

Kerem atam