[英]How to run multiple powershell commands in scripts for package.json
[英]How to run multiple PowerShell commands with package.json scripts
我的package.json
中有以下脚本定义“调试窗口”:
{
"scripts": {
"debug-windows": "$env:NODE_ENV=\"dev\"; node src/dequeue.js"
}
}
我使用npm run debug-windows
并收到错误消息:
> servicebus-timeout@1.0.0 debug-windows C:\myapp
> $env:NODE_ENV="dev"; node src/dequeue.js
The filename, directory name, or volume label syntax is incorrect.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! servicebus-timeout@1.0.0 debug-windows-test: `$env:NODE_ENV="dev"; node src/dequeue.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the servicebus-timeout@1.0.0 debug-windows-test 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\<USERNAME>\AppData\Roaming\npm-cache\_logs\2020-04-27T16_29_32_585Z-debug.log
如果我直接在 PowerShell 上运行相同的命令,它会成功:
PS C:\myapp> $env:NODE_ENV="dev"; node src/dequeue.js
Waiting for messages: Mon Apr 27 2020 13:24:06 GMT-0300 (Brasilia Standard Time)
我花了一些时间检查 npm 文档并发现了问题。 npm-run-script
在 Windows 上运行 CMD,而不是 PowerShell。
解决方案是使用&
和适当的set
分隔命令以定义 session 变量:
"debug-windows": "set NODE_ENV=dev & node src/dequeue.js"
我没有必要让npm使用PowerShell,只是我以为是这样做的。
如果出于其他原因想要运行 PowerShell,请查看这个有很多选项的线程。
关于npm-run-script 的文档:
您的脚本在其中运行的实际 shell 取决于平台。 默认情况下,在类 Unix 系统上是 /bin/sh 命令,在 Windows 上是 cmd.exe。 /bin/sh 所指的实际 shell 也取决于系统。 从 npm@5.1.0 开始,您可以使用 script-shell 配置自定义 shell。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.