In this post, we talk about the nx: command not found error and its solution. To fix this problem, you can use the prefix ‘npx‘ when executing the install command, or the package must be installed globally. Continue with the content below for further information.

## Why does this error happen?

NX is one of the most advanced build systems out there and offers some compelling features to take your codebase to the next level.

You run the command “nx build my-application” and catch the error “nx: command not found“. This error message means your device cannot find the ‘nx‘ command. That’s because you forgot the ‘npx‘ keyword when executing the above statement, or it is very likely that you do not have the @nrwl/cli package installed.

The most straightforward approach to get around this trouble is to add the keyword ‘npx‘ at the beginning of the statement. For example:

npx nx

:: graph dependencies within the workspace
npx graph

:: connect workspace to Nx Cloud
npx connect


Or another method for you is globally installing the ‘@nrwl/cli‘ package.

You must execute the install command with the ‘-g‘ flag.

npm install -g @nrwl/cli

If you cannot install ‘@nrwl/cli‘ with the above command, try executing that command again with admin/superuser privileges.

After a successful installation, we strongly recommend restarting the terminal before continuing

If you have tried the above methods, but it doesn’t work, please run the command below:

npm config get prefix

Output:

C:\Users\HoanBM\AppData\Roaming\npm

You will get a string as a path. This path takes you to the location where ‘npm‘ stores the packages you have installed globally. Here you can find those packages in the bin folder.

On your machine, what you have to do now is look for the PATH environment variable to see if it has the same value as the path you just got from the command ‘npm config get prefix‘. If not, please add that path for the PATH environment variable.

Note: If you have tried all the above methods and the error persists, remove NodeJS from your machine and reinstall it. Re-execute command ‘npm install -g @nrwl/cli‘ to install Nx globally.

## Summary

To sum up, to solve the nx: command not found error you must install the package globally or add the ‘npx’ keyword to the beginning of the install command. We hope the above methods can help you with your problem.

Maybe you are interested: