The mocha command is not found even though npm installed it
mocha: command not foundShort answer
npm puts a local install's executables in ./node_modules/.bin, which is not on your shell PATH, and a global install in the prefix bin folder, which may not be. Run npx mocha, or add a test script and run npm test. For a global install, add the prefix bin folder to PATH.
Fix it fast
1Find out which cause you have
Find out where mocha was installed: in this project, or globally. The first command lists the local copy, the second the global ones.
npm ls mocha npm ls -g --depth=02Match what you see, then jump to its fix
What does the error look like?
mocha: command not found
Cause 1 of 2
Mocha is installed in the project, and you typed mocha in the shell
1Check
Local executables are linked into ./node_modules/.bin so npm scripts can use them. A terminal outside npm does not look there.
ls node_modules/.bin | grep mocha2Fix
Run it through npx, or define a test script in package.json (the usual setup) and run that.
# package.json
"scripts": { "test": "mocha" }
# then
npm test
# or one-off
npx mocha3Verify
Mocha starts and reports the result of your tests.
npx mochaConfirmed by mochajs.org
Cause 2 of 2
Mocha was installed globally, but the global bin folder is not on PATH
1Check
A global install links executables into the bin folder under the npm prefix, and that folder must be on PATH.
npm prefix -g
echo $PATH2Fix
Add the bin folder under the prefix to PATH in your shell profile, then open a new terminal. A project-local install with npx is the simpler long-term choice.
# ~/.zshrc or ~/.bashrc, using the path printed by npm prefix -g
export PATH="$(npm prefix -g)/bin:$PATH"3Verify
In a new terminal, mocha is found.
which mochaConfirmed by docs.npmjs.com
How do I stop it coming back?
- Install Mocha as a devDependency and call it from the test script, so every machine and CI runner uses the same version.
- Run project tools with npx or npm scripts and keep global installs for tools you truly use everywhere.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
You installed nodemon but the shell says it cannot find it
nodemon: command not found
The shell only finds programs in folders listed in PATH.
3 causes, each with a fixBack end
Mocha async test fails with a 2000ms timeout
Error: Timeout of 2000ms exceeded
Mocha gives every test and hook 2000 milliseconds by default.
3 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fix