nodemon ''mocha' is not recognized as an internal or external command, operable program or batch file
Asked Answered
S

10

17

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...
Setiform answered 27/5, 2017 at 16:12 Comment(1)
Anyone coming here from searching the error message running mocha directly (not using nodemon), I had this suddenly on Windows despite running npm i. I had to explicitly run npm i -D mocha again, then it worked.Meekins
S
37

That worked fine with the line:

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

in package.json

Setiform answered 27/5, 2017 at 16:15 Comment(2)
thanks..why the first syntax work for only some people?Garett
Its a windows only issueMelleta
C
7

install mocha globally then it will work

npm install -g mocha --save-dev

Concomitant answered 22/2, 2019 at 13:57 Comment(0)
I
5

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

Irrational answered 8/7, 2018 at 10:18 Comment(0)
C
1

Also, check your NODE_ENV=development if you are on Windows and are using git-bash. For some reason, it defaults to production.

$ echo $NODE_ENV

With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in "devDependencies"

You can verify this by checking your node_modules/ folder and see if mocha was installed. If not:

$ npm install --only=dev

also:

$ NODE_ENV=development
$ npm i -D mocha

would do the trick.

Capacitance answered 29/1, 2018 at 18:12 Comment(1)
wow I am working with a company who refuses to give me a MacOS to work with so your solution was helpful.Breslau
Q
1
"test": "mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""

For Run

npm run test-watch
Quince answered 26/3, 2018 at 9:57 Comment(1)
How is it relevant to the question?Zolazoldi
F
1

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...

Friendship answered 20/9, 2018 at 13:12 Comment(0)
A
1

for executing npm test command install mocha globally with -g

command: npm install mocha -g

I had the same issue and worked after installing mocha globally. provided you have all dependency ready in your project

Arneson answered 17/11, 2021 at 6:10 Comment(0)
H
0

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.

Hyponitrite answered 27/5, 2017 at 16:12 Comment(0)
H
0

Inside the package.json, you need to add a new script right after the "test" script. We can create a custom script and named it as "test-watch" and the value of the "test-watch"is "nodemon --exec \"npm test\""(i.e "test-watch": "nodemon --exec \"npm test\"") After this step we can use npm run test-watch command in terminal.

Hipbone answered 2/7, 2018 at 13:36 Comment(0)
Q
0

Use "npm run test" and the command should be "nodemon --exec "mocha -R min"". For me, it worked when the previous command is used instead of the npm test & "nodemon --exec 'mocha -R min'"

Quahog answered 6/4, 2021 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.