Please stick with us in this article; you will find the cause of the lerna: command not found error and its solution. To resolve this problem, you will be instructed to install the Lerna package globally or use the ‘npx’ keyword at the beginning of each command.

## Why does this error happen?

Lerna is a brief, modern build system. It is used for managing and publishing multiple TypeScript or JavaScript in the same repository.

Suppose you want to create a new Lerna repository or upgrade an existing repository to the current version of Lerna. You run the ‘lerna init’ command and get the lerna: command not found error. You get this error message because you forgot the ‘npx’ prefix when running the commands or you don’t have your project’s Lerna package installed globally.

The most accessible way to this problem is adding the ‘npx‘ keyword to the beginning of the commands before you run them. Here are some examples of command execution:

:: list all local packages
npx lerna list

:: import a package into the monorepo with the commit history
npx lerna commit

:: publish all packages in the current project
npx lerna import


You can apply this method to any command in Lerna.

Alternatively, you can choose to install the Lerna package globally. To do that, run the install command with the ‘-g‘ flag as below:

npm install -g lerna

After executing the above command, you can check the version you have installed of Lerna:

lerna --version

Output:

6.0.3

If you also get the version of Lerna in the output, it means the installation was successful.

If the error still needs to be resolved after you have tried both of the above approaches. Let’s run this command:

npm config get prefix

This statement will return you a string. This string is the path to where the global packages are being located by ‘npm‘. You can find these packages in the bin directory of the specified path.

What you need to do is checking the PATH environment variable on the device you are using. If the PATH environment variable does not contain the return value of the ‘npm config get prefix’ command, set that value for it.

Note: If all methods are not working, let’s try uninstalling NodeJS on your machine and then reinstalling the latest version of NodeJS. To install the Lerna package globally, execute the command ‘npm install -g lerna’ again.

## Summary

That’s the end of this post. To sum up, you can use the ‘npx’ prefix or install the Lerna package globally in your project to fix the lerna: command not found error. Hopefully, the information we mention below will help you.

Maybe you are interested: