简体   繁体   English

如何使用 package.json 脚本运行多个 PowerShell 命令

[英]How to run multiple PowerShell commands with package.json scripts

I have the following script definition "debug-windows" in my package.json :我的package.json中有以下脚本定义“调试窗口”:

{
    "scripts": {
        "debug-windows": "$env:NODE_ENV=\"dev\"; node src/dequeue.js"
    }
}

Which I run using npm run debug-windows and I get the error:我使用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

If I run the same command directly on PowerShell it succeeds:如果我直接在 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)

I spent some time checking the npm docs and found out the problem.我花了一些时间检查 npm 文档并发现了问题。 npm-run-script runs CMD on Windows, and not PowerShell. npm-run-script在 Windows 上运行 CMD,而不是 PowerShell。

The solution was to separate the commands using & and the appropriate set to define session variables:解决方案是使用&和适当的set分隔命令以定义 session 变量:

"debug-windows": "set NODE_ENV=dev & node src/dequeue.js"

I have no need for npm to use PowerShell, it's just that I thought it was doing so.我没有必要让npm使用PowerShell,只是我以为是这样做的。

If one wants to run PowerShell for other reasons, check this thread which has a lot of options.如果出于其他原因想要运行 PowerShell,请查看这个有很多选项的线程

Documentation on npm-run-script :关于npm-run-script 的文档:

The actual shell your script is run within is platform dependent.您的脚本在其中运行的实际 shell 取决于平台。 By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe.默认情况下,在类 Unix 系统上是 /bin/sh 命令,在 Windows 上是 cmd.exe。 The actual shell referred to by /bin/sh also depends on the system. /bin/sh 所指的实际 shell 也取决于系统。 As of npm@5.1.0 you can customize the shell with the script-shell configuration.从 npm@5.1.0 开始,您可以使用 script-shell 配置自定义 shell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM