考虑一下app.js
const { doCoolStuff } = require("./api/myApi");
// grab param from command line into "myParam"
doCoolStuff(myParam);
... // more code 和Package.json
{
"name": "-------",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"send": "node app.js"
},
...
}如何在运行app.js时将参数传递给npm run send?
有点像npm run send myEmail@myDomain.com
发布于 2021-02-22 10:37:04
const params = process.argv.slice(2)现在,这个param变量具有通过npm命令传递的所有参数。
npm run send myEmail@domain.com
params[0] // this will provide you with the value myEmail@domain.com发布于 2021-02-22 10:44:54
Npm将解析传递给脚本的任何参数,除非它是在后面传递的--后面是空格。在npm解析它们之后,它们将在npm_config_环境变量中可用。
{
"scripts": {
"send": "echo \"send email to $npm_config_name @ $npm_config_mail "
}
}那就跑
npm run send --mail=xyz.com --name=sample输出:send email to sample@xyz.com
发布于 2021-02-22 10:28:54
使用process.argv访问脚本中的参数。
至于如何指定参数,请参见:Sending command line arguments to npm script
https://stackoverflow.com/questions/66313877
复制相似问题