mocha: command not found error [Solved]

avatar

Borislav Hadzhiev

Last updated: Mar 8, 2022

banner

Photo from Unsplash

mocha: command not found error [Solved] #

Use npx to solve the error "mocha: command not found", e.g. npx mocha or install the package globally by running npm install -g mocha to be able to use the command without the npx prefix.

command not found mocha

The fastest way to solve the error is to use the npx command.

shell
npx mocha npx mocha --version

If the error is not resolved, you can force the command to use the latest version of mocha.

shell
npx mocha@latest npx mocha@latest --version

The commands above use the latest version of mocha.

Alternatively, you can install mocha globally or as a development dependency.

shell
# 👇️ installs mocha globally (can run from any directory) npm install -g mocha # 👇️ installs mocha locally to the project (must be run from root directory) npm install --save-dev mocha

The benefit of installing mocha as a development dependency is that you can control the version of the package in your package.json file.

You can also create a test command in the scripts object of your package.json file.

package.json
{ "scripts": { "test": "mocha" } }

Now you would run the command as npm run test and not use mocha directly.

If the global installation of mocha fails, you might have to run the command prefixed with sudo.
shell
# 👇️ if you get permissions error sudo npm install -g mocha mocha --version

You can link your project to the globally installed mocha package, by opening your terminal in your project's root directory (where your package.json file is) and running the npm link mocha command.

shell
npm link mocha

The npm link command creates a symbolic link from the globally installed package to the node_modules/ directory of the current folder.

If the "mocha: command not found" error is not resolved, try restarting your terminal.

If that doesn't help, run the following command:

shell
npm config get prefix

The command will show you the path where npm puts your globally installed packages. The global packages will be in the bin directory at the specified path.

Look at the PATH environment variable on your operating system and add the path that the npm config get prefix command outputs if it's not already there.

If you add the output from the command to your PATH environment variable, you have to restart any open command prompts before it takes effect.

If that didn't work, try to add the path to the bin folder (from npm config get prefix) to your PATH environment variable and restart your terminal.

For example, on macOS, you can update your path with the following command:

shell
# make sure path matches with npm config get prefix export PATH=/usr/local/share/npm/bin:$PATH

And on Windows, the output of the npm config get prefix command will look something like: C:\Users\Your_User_Name\AppData\Roaming\npm.

Edit the environment variable on your machine and add the specified path (replacing the placeholder with your username).

If you are on Linux, you can add the output from the npm config get prefix command to your .bashrc file.

~/.bashrc
# 👇️ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
If you add the output from the command to your PATH environment variable, you have to restart any open command prompts before it takes effect.

If that doesn't help try to reinstall Node.js on your machine and then install mocha globally by running npm install -g mocha@latest.

During the installation, you might get a prompt for whether you want to automatically update the PATH environment variable on your system, make sure to tick the option.
shell
# 👇️ installs mocha globally (can run from any directory) npm install -g mocha # 👇️ installs mocha locally to the project (must be run from root directory) npm install --save-dev mocha
If the global installation of mocha fails, you might have to run the command prefixed with sudo.
shell
# 👇️ if you get permissions error sudo npm install -g mocha mocha --version

Alternatively, you can see how you can fix the permissions error on this page in the official npm docs.

Conclusion #

Use npx to solve the error "mocha: command not found", e.g. npx mocha or install the package globally by running npm install -g mocha to be able to use the command without the npx prefix.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.