nodemon ''mocha' is not recognized as

2020-03-12 03:24发布

Running a test for a nodejs project on windows 10 with the line in package.json as:

"test": "nodemon --exec 'mocha -R min'"

I get:

>  nodemon --exec 'mocha -R min'  

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `'mocha -R min'`
''mocha' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
rs
[nodemon] starting `'mocha -R min'`
''mocha' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...

8条回答
倾城 Initia
2楼-- · 2020-03-12 04:03

I am no Windows kernel or any .. expert. In my case the test script kept erroring out with the message npm is not recognized as an Internal or External command.

a) When I had it as

"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec 'npm test'"

It ran a few times and stopped and the error started occurring so when I switched to

"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""

Still I kept getting the same error of npm not recognized... And no matter how many times I issued Ctrl c, the nodemon wouldn't stop.

I did take the steps of restarting my laptop, uninstalled and re-installed nodeJs, updated the PATH variable in Control Panel - User Accounts - Environment variables all amounting to no end in sight.

This leads me to believe that somewhere or somehow, either nodemon or mocha not sure, what is hanging, so even after I had modified to escape and use double quotes as in

"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""

I still kept getting the same error.

b) So then I changed the name of the key from test-watch to test-new

"test": "mocha **/*.test.js",
"test-new": "nodemon --exec \"npm test\""

and ran npm run test-new and every tests runs fine.

Go figure...

So I think I will stick to keeping unique test script names between different projects. I have no other explanation.... Anyone can shed light on this? Please do so...

查看更多
【Aperson】
3楼-- · 2020-03-12 04:05

That worked fine with the line:

"test": "nodemon --exec \"mocha -R min\""

in package.json

查看更多
Root(大扎)
4楼-- · 2020-03-12 04:07

install mocha globally then it will work

npm install -g mocha --save-dev

查看更多
我只想做你的唯一
5楼-- · 2020-03-12 04:08

If you are using windows OS the do not use the single quotes

"test": "nodemon --exec 'mocha -R min'"

Use this

"test": "nodemon --exec mocha -R min"

Visit: www.mycodingx.com for more

查看更多
▲ chillily
6楼-- · 2020-03-12 04:10

An alternative approach is to add the mocha path to the environment variables then restart the bash On your editor, navigate to the bin folder of mocha and add both paths to your system environments. All script options that have been illustrated work with this approach

"scripts": {
    "test": "nodemon --exec \"mocha -R min\""
}

or

"scripts": {
    "test": "nodemon --exec 'mocha -R min'"
}

or

"scripts": {
    "test": "nodemon --exec mocha -R min"
 }

in the package.json file are correct dependencies definition

I hope this helps fix the issue.

查看更多
叼着烟拽天下
7楼-- · 2020-03-12 04:13
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""

For Run

npm run test-watch
查看更多
登录 后发表回答