【问题标题】:How to add Powershell commands to an NPM script?如何将 Powershell 命令添加到 NPM 脚本?
【发布时间】:2019-11-29 17:40:02
【问题描述】:

这适用于 Powershell 命令行:

C:\rootProject\> copy ZZZ.js -destination ZZZXXX.js

但这不起作用:

package.json

"scripts": {
  "copy-script": "copy ZZZ.js -destination ZZZXXX.js"
}

运行脚本:

C:\rootProject\> npm run copy-script

错误

The syntax of the command is incorrect.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test-module-imports@1.0.0 test-copy: `copy ZZZ.js -destination ZZZXXX.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test-module-imports@1.0.0 test-copy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\USER\AppData\Roaming\npm-cache\_logs\2019-11-29T09_29_59_261Z-debug.log

【问题讨论】:

    标签: node.js powershell npm npm-scripts


    【解决方案1】:

    它需要在脚本内的命令之前加上powershell 术语。

    package.json

    "scripts": {
      "copy-script": "powershell copy ZZZ.js -destination ZZZXXX.js"
    }
    

    现在可以了:

    C:\rootProject\> npm run copy-script
    

    【讨论】:

    • 如果你经常创建 npm 脚本;跨平台支持不是必需的,并且您经常使用 Powershell 特定命令,然后考虑通过设置 script-shell 配置将 npm 在 Windows 上使用的默认 cmd.exe shell 更改为 pws。如何做到这一点的例子可以在this answer 中找到。您可能会否定在命令前添加 powershell 术语的需要。
    猜你喜欢
    • 2019-08-20
    • 2014-02-08
    • 2011-10-31
    • 2018-03-28
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    相关资源
    最近更新 更多