如何在 npm 脚本中获取环境变量?
How to get environment variable in npm script?
我正在尝试访问 npm 脚本本身中的环境变量,如下所示:
"scripts": {
"test": "istanbul cover node_modules/.bin/_mocha --root ../SERVER/routes -- --recursive"
},
然后像这样启动这个脚本:
SERVER=somewhere npm test
如何在 package.json 本身的 npm 脚本中获取 SERVER
变量的解析值?
使用 $SERVER
对你有用吗?
"scripts": {
"test": "istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive"
}
对于 windows 用户,您可以像这样使用变量:%SERVER%
而不是 $SERVER
。
或者使用 cross-env
module 的更好方法,这将允许您在所有平台上像 linux 那样做:
npm i cross-env
并使用它:
"scripts": {
"test": "cross-env-shell \"istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive\""
}
我正在尝试访问 npm 脚本本身中的环境变量,如下所示:
"scripts": {
"test": "istanbul cover node_modules/.bin/_mocha --root ../SERVER/routes -- --recursive"
},
然后像这样启动这个脚本:
SERVER=somewhere npm test
如何在 package.json 本身的 npm 脚本中获取 SERVER
变量的解析值?
使用 $SERVER
对你有用吗?
"scripts": {
"test": "istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive"
}
对于 windows 用户,您可以像这样使用变量:%SERVER%
而不是 $SERVER
。
或者使用 cross-env
module 的更好方法,这将允许您在所有平台上像 linux 那样做:
npm i cross-env
并使用它:
"scripts": {
"test": "cross-env-shell \"istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive\""
}