Kou*_*sha 5 javascript node.js npm
这是我的 npm package.json scripts:
"scripts": {
"method1": "./somScript $1",
"method2": "webpack-dev-server",
"runBoth": "npm run method1 $1 & npm run method2"
},
Run Code Online (Sandbox Code Playgroud)
我希望能够运行npm run runBoth someArgument它会传递给runBoth,然后传递给method1.
但是,我上面的代码不起作用。请指教!
不,不幸的是这是不可能的,因为 NPM 附加了参数。
您正在执行: npm run method1 & npm run method2 "someArgument"
所以,我猜你的输出如下:
[nodejs@mean test]$ npm run runBoth someArgument
> stackOverflow@0.0.1 runBoth /data/nodejs/node_projects/test
> npm run method1 $1 & npm run method2 "someArgument"
> stackOverflow@0.0.1 method1 /data/nodejs/node_projects/test
> ./somScript
[somScript] argument:
> stackOverflow@0.0.1 method2 /data/nodejs/node_projects/test
> ./script2.sh "someArgument"
now executing script2.sh using arg: someArgument
[nodejs@mean test]$
Run Code Online (Sandbox Code Playgroud)
另请注意,应使用“--”传递参数,如下所示:
[nodejs@mean test]$ npm run runBoth -- someArgument
> stackOverflow@0.0.1 runBoth /data/nodejs/node_projects/test
> npm run method1 $1 & npm run method2 "someArgument"
> stackOverflow@0.0.1 method1 /data/nodejs/node_projects/test
> ./somScript
[somScript] argument:
> stackOverflow@0.0.1 method2 /data/nodejs/node_projects/test
> ./script2.sh "someArgument"
now executing script2.sh using arg: someArgument
[nodejs@mean test]$
Run Code Online (Sandbox Code Playgroud)
但就您而言,预期输出将是相同的。
我的package.json:
"scripts" : {
"method1": "./somScript",
"method2": "./script2.sh",
"runBoth": "npm run method1 $1 & npm run method2 "
}
Run Code Online (Sandbox Code Playgroud)
您应该尝试其他实现(如导出/使用环境变量或其他)
这是一个可能的解决方法:
包.json:
"scripts" : {
"method1": "./somScript",
"method2": "./script2.sh",
"runBoth": "npm run method1 $1 & npm run method2 ",
"runBoth2": "./alternative.sh"
}
Run Code Online (Sandbox Code Playgroud)
其中runBoth2执行“ Alternative.sh ”:
[nodejs@mean test]$ cat alternative.sh
npm run method1 $1 & npm run method2
Run Code Online (Sandbox Code Playgroud)
以及预期的输出:
[nodejs@mean test]$ npm run runBoth2 -- someArgument
> stackOverflow@0.0.1 runBoth2 /data/nodejs/node_projects/test
> ./alternative.sh "someArgument"
> stackOverflow@0.0.1 method1 /data/nodejs/node_projects/test
> ./somScript "someArgument"
[somScript] argument: someArgument
> stackOverflow@0.0.1 method2 /data/nodejs/node_projects/test
> ./script2.sh
now executing script2.sh using arg:
[nodejs@mean test]$
Run Code Online (Sandbox Code Playgroud)
问候
| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |