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



我在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 在调用它的终端环境中运行脚本?

脚本始终在默认的 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"
},

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

另请参阅:

  • 为什么"DEBUG=foo node index.js"在"package.json"的"脚本"部分中失败

最新更新