cygwin / windows下的NPM脚本:命令的语法不正确 [英] NPM script under cygwin/windows: the syntax of the command is incorrect
问题描述
我在Windows 7计算机上运行节点6.9.5和NPM 3.10.10。我的终端是Cygwin 2.877。
如果我尝试在Cygwin中运行以下命令,它可以正常工作:
mkdir mydir / mysubdir;
但是,如果我把它放到 package.json
file,例如:
scripts:{
test:mkdir mydir / mysubdir
},
并执行:
$ b b
npm run test
/ p>
命令的语法不正确。
Googling之后,它似乎是一个Windows命令提示符错误,而不是Cygwin。因此,NPM似乎试图使用命令提示符而不是现有的Cygwin环境来运行脚本。
如何解决这个问题?或者,我如何确保NPM在从其调用的终端环境中运行脚本?
脚本总是
如果你想要在bash中运行,那么把它放在 package.json
:
scripts:{
test:bash test.sh
},
并将其放在 test.sh
:
#!/ bin / bash
mkdir mydir / mysubdir
或者,如csvan在注释中指出的,您可以使用Node脚本而不是shell脚本:
scripts:{
test:node test.js
},
此方法对跨平台兼容性更好。
另请参阅:
I am running Node 6.9.5 and NPM 3.10.10 on a Windows 7 machine. My terminal is Cygwin 2.877.
If I try to run the following in Cygwin, it works fine:
mkdir mydir/mysubdir;
However, if I put it into a package.json
file instead, e.g.:
"scripts": {
"test": "mkdir mydir/mysubdir"
},
and run:
npm run test
It fails with:
The syntax of the command is incorrect.
After Googling the above, it seems to be a Windows Command Prompt error, not a Cygwin one. As such, it seems that NPM is trying to run the script using the Command Prompt rather than the existing Cygwin environment.
How can I fix this? Or rather, how can I make sure NPM runs scripts in the terminal environment it is being invoked from?
The script are always run in the default windows shell, not cygwin.
If you want it to run in bash then put this in package.json
:
"scripts": {
"test": "bash test.sh"
},
and put this in test.sh
:
#!/bin/bash
mkdir mydir/mysubdir
Or, as csvan pointed out in the comment, you can use Node scripts instead of shell scripts:
"scripts": {
"test": "node test.js"
},
This approach is even better for cross-platform compatibility.
See also:
这篇关于cygwin / windows下的NPM脚本:命令的语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!