【问题标题】:How to run multiple PowerShell commands with package.json scripts如何使用 package.json 脚本运行多个 PowerShell 命令
【发布时间】:2020-08-11 07:04:11
【问题描述】:

我的package.json 中有以下脚本定义“debug-windows”:

{
    "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)

【问题讨论】:

    标签: node.js powershell npm


    【解决方案1】:

    我花了一些时间检查 npm 文档并发现了问题。 npm-run-script 在 Windows 上运行 CMD,而不是 PowerShell。

    解决方案是使用&amp; 和适当的set 将命令分开以定义会话变量:

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

    我不需要 npm 使用 PowerShell,只是我认为它正在这样做。

    如果出于其他原因想要运行 PowerShell,请查看this thread,它有很多选项。

    npm-run-script 上的文档:

    运行脚本的实际 shell 取决于平台。经过 默认,在类 Unix 系统上是 /bin/sh 命令,在 Windows 上是 是cmd.exe。 /bin/sh 所指的实际外壳也取决于 系统上。从 npm@5.1.0 开始,您可以使用 脚本外壳配置。

    【讨论】:

    • 对于跨平台解决方案(即对于 windows cmd*nix sh)使用 cross-env
    • @RobC 我不知道那个!我一定会尝试package.json 和其他自动化。
    猜你喜欢
    • 2019-08-15
    • 2020-05-10
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2020-02-25
    • 2020-07-27
    相关资源
    最近更新 更多