如何将 Powershell 命令添加到 NPM 脚本中?

cbd*_*per 4 powershell node.js npm npm-scripts

这适用于 Powershell 命令行:

C:\rootProject\> copy ZZZ.js -destination ZZZXXX.js
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

包.json

"scripts": {
  "copy-script": "copy ZZZ.js -destination ZZZXXX.js"
}
Run Code Online (Sandbox Code Playgroud)

运行脚本:

C:\rootProject\> npm run copy-script
Run Code Online (Sandbox Code Playgroud)

错误

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
Run Code Online (Sandbox Code Playgroud)

cbd*_*per 6

powershell脚本内的命令之前需要该术语。

包.json

"scripts": {
  "copy-script": "powershell copy ZZZ.js -destination ZZZXXX.js"
}
Run Code Online (Sandbox Code Playgroud)

现在它可以工作了:

C:\rootProject\> npm run copy-script
Run Code Online (Sandbox Code Playgroud)

  • 如果您经常创建 npm 脚本;跨平台支持不是必需的,并且您经常使用 Powershell 特定命令,然后考虑通过设置 [`script-shell`](https: //docs.npmjs.com/misc/config#script-shell)配置。有关如何执行此操作的示例,请参阅[此答案](/sf/ask/3871029501/ -windows?#answer-55303370)。您可能不需要在命令前添加“powershell”术语。 (3认同)