Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
932 views
in Technique[技术] by (71.8m points)

node.js - NPM script under cygwin/windows: the syntax of the command is incorrect

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...