Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The mocha command is not found even though npm installed it

Errormocha: command not found

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

  • Applies to npm 10, Node.js 22, Mocha
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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=0
  2. 2Match what you see, then jump to its fix

    1. 1npm ls mocha shows it under your project, and ./node_modules/.bin/mocha existsMocha is installed in the project, and you typed mocha in the shellFix
    2. 2npm ls -g shows mocha, yet the shell cannot find itMocha was installed globally, but the global bin folder is not on PATHFix

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 mocha

2Fix

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 mocha

3Verify

Mocha starts and reports the result of your tests.

npx mocha

Confirmed by mochajs.org

Did this fix it?

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 $PATH

2Fix

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 mocha

Confirmed by docs.npmjs.com

Did this fix it?

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

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 fix

Back 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.