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

4
这个可以在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
1个回答

6

在脚本内部的命令前,需要加上powershell关键字。

package.json

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

现在它可以工作了:
C:\rootProject\> npm run copy-script

3
如果你经常创建npm脚本,其中不需要跨平台支持,并且经常使用PowerShell特定命令,则考虑更改npm在Windows上使用的默认cmd.exe shell为pws,方法是设置script-shell配置。如何执行此操作的示例可以在此答案中找到。这样做后,你可能不再需要在命令之前添加powershell术语。 - RobC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接