【问题标题】:Passing command line arguments to npm 'pre' script and script with multiple commands将命令行参数传递给 npm 'pre' 脚本和带有多个命令的脚本
【发布时间】:2017-07-18 09:19:29
【问题描述】:

有没有办法将命令行参数传递给 npm 'pre' 脚本或运行多个命令的脚本?

假设一个简单的脚本 mySexyScript.js 只是注销 process.argv :

console.log(process.argv);

这行得通

使用 npm 脚本:

...
"scripts": {
    ....
    "sexyscript": "node mySexyScript.js"
    ....
}
...

运行:

npm run sexyscript -- --foo=bar

参数按预期记录到控制台。

'pre' 脚本 - 这不起作用

使用 npm 脚本:

...
"scripts": {
    ....
    "presexyscript": "node mySexyScript.js"
    "sexyscript": "node mySuperSexyScript.js"
    ....
}
...

运行:

npm run sexyscript -- --foo=bar

参数未传递给 mySexyScript 且未记录

多个命令 - 这也不起作用

使用 npm 脚本:

...
"scripts": {
    ....
    "sexyscript": "node mySexyScript.js && node mySuperSexyScript.js"
    ....
}
...

运行:

npm run sexyscript -- --foo=bar

参数未传递给 mySexyScript 且未记录

【问题讨论】:

    标签: node.js npm npm-scripts


    【解决方案1】:

    没有办法以您描述的方式传递参数。

    假设package.json

    ...
    "scripts": {
        ....
        "somescript": "node one.js && node two.js"
        ....
    }
    ...
    

    跑步:

    npm run somescript -- --foo=bar
    

    基本上只是运行

    node one.js && node two.js --foo=bar
    

    在默认系统 shell 上(通常是 bashcmd.exe)。

    npm 实际上对 shell 运算符一无所知(即&&),因此它无法将 args 传递给两个脚本。

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 2016-05-27
      • 2018-01-11
      • 2014-03-08
      • 2013-10-22
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多