[英]Setting custom variables from npm script to javascript
Might be a duplicate but is there a way to set variables from an npm script that are accessible in javascript via something like process.env.CUSTOM_VAR
?可能是重复的,但有没有办法从 javascript 中通过类似process.env.CUSTOM_VAR
访问的 npm 脚本设置变量?
In my package.json in the scripts section I've added "start": "react-native start --reset-cache"
which works fine but now I'd like to do something like this "inspect": "INSPECT=1 react-native start --reset-cache"
and have it accessible in javascript.在我的 package.json 的脚本部分中,我添加了"start": "react-native start --reset-cache"
效果很好,但现在我想做这样的事情"inspect": "INSPECT=1 react-native start --reset-cache"
并在 javascript 中访问它。 I've googled around everything seems to point to adding it like above but it doesn't seem to be working.我用谷歌搜索了一切似乎都指向像上面那样添加它,但它似乎不起作用。
Any help is appreciated!任何帮助表示赞赏!
You can have a program to read the process.argv
and do things based on that list(process.argv includes the flags that the script started on)您可以有一个程序来读取process.argv
并根据该列表执行操作(process.argv 包括脚本启动的标志)
argvToEnv.js argvToEnv.js
//you can set the prefix to your liking
const prefix="*" //the start script may have many flags
//if any flag starts with a prefix, it will turn it into an env variable
let envList={} //soon to be list of things env variables
for(let i=0;i<process.argv.length;i++){
let flag=process.argv[i]
if(flag.startsWith(prefix)){
envList[process.argv[i++].substr(prefix.length)]=process.argv[i]
}
}
Object.keys(envList).forEach(key=>{
process.env[key]=envList[key]
})
index.js index.js
require('path/to/argvToEnv.js')
//whatever else you do in your main js file
start script启动脚本
"start": "react-native start --reset-cache *CUSTOM_VAR \"success hehe\""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.