cygwin/windows下的NPM脚本:命令语法不正确

问题描述 投票:0回答:2

我在 Windows 7 计算机上运行 Node 6.9.5 和 NPM 3.10.10。我的终端是 Cygwin 2.877。

如果我尝试在 Cygwin 中运行以下命令,它工作正常:

mkdir mydir/mysubdir;

但是,如果我将其放入

package.json
文件中,例如:

"scripts": {
 "test": "mkdir mydir/mysubdir"
},

然后运行:

npm run test

它失败了:

The syntax of the command is incorrect.

谷歌搜索上述内容后,这似乎是 Windows 命令提示符错误,而不是 Cygwin 错误。因此,NPM 似乎正在尝试使用命令提示符而不是现有的 Cygwin 环境来运行脚本。

我该如何解决这个问题?或者更确切地说,我如何确保 NPM 在调用它的终端环境中运行脚本?

node.js windows npm cygwin
2个回答
8
投票

脚本始终在默认的 Windows shell 中运行,而不是 cygwin。

如果您希望它在 bash 中运行,请将其放入

package.json
:

"scripts": {
    "test": "bash test.sh"
},

并将其放入

test.sh

#!/bin/bash
mkdir mydir/mysubdir

或者,正如 csvan 在评论中指出的那样,您可以使用 Node 脚本而不是 shell 脚本:

"scripts": {
    "test": "node test.js"
},

这种方法对于跨平台兼容性来说甚至更好。

另请参阅:


0
投票

解决方案如下:

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
© www.soinside.com 2019 - 2024. All rights reserved.