Last updated: Apr 4, 2024
Reading timeยท6 min
Note: if you got the error 'npx' is not recognized as an internal or external command on Windows, click on the second subheading.
The "npx: command not found" error occurs when the npx
package is not
installed on macOS or Linux or you haven't added Node to your PATH environment
variable.
To solve the error, install npx
and add Node to your PATH environment
variable.
Open your terminal and run the following command to install npx
.
npm install -g npx
If you get a permissions error, prefix the command with sudo.
sudo npm install -g npx
Note: if you got the error 'npx' is not recognized as an internal or external command on Windows, click on the following subheading.
If the error persists, run the following command.
npm config get prefix
The command will show you the path where your globally installed packages are stored.
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 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:
# make sure the `path` matches with `npm config get prefix` output export PATH=/usr/local/share/npm/bin:$PATH
If you are on Linux, you can add the output from the npm config get prefix
command to your .bashrc
file.
# ๐๏ธ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
Install npx
by running the following command.
npm install -g npx
If you get a permissions error, prefix the command with sudo
.
sudo npm install -g npx
If the error persists, reinstall Node.js on your machine.
You might get prompted whether you want to add Node.js to your PATH environment variable during the installation.
Make sure to check the checkbox before proceeding.
The error "'npx' is not recognized as an internal or external command, operable program or batch file" occurs for 2 main reasons:
PATH
environment variable.Depending on your operating system, the error message might be:
If you have Node.js installed and added to your PATH environment variable, use
the following command to install npx
.
# ๐๏ธ for Windows npm install -g npx # ๐๏ธ for macOS or Linux sudo npm install -g npx
npx
, run CMD as an administrator and rerun the command.To open CMD as an administrator:
Click on the Search bar and type CMD.
Right-click on the Command Prompt application and click "Run as administrator".
You can issue the npx --version
command to check if npx
is installed.
npx --version
npm
to your PATH environment variable.To install Node and add it to your PATH
environment variable:
Next
on the Welcome screen.Next
.Next
.Next
. Notice that there is an "Add to
PATH" option selected.Next
.Install
button.Finish
button.Make sure to close any existing command prompts and PowerShell instances for
the PATH
variable to get updated.
You can start a new CMD shell and use the npx --version
command to make
sure npx
is installed.
npx --version node --version
npx
commands for the PATH variable to update.If you got the error in Visual Studio Code and restarting VSCode doesn't help, try to click on the trash bin button to restart the terminal.
If the error persists, add Node to your PATH environment variable manually.
To add Node to your PATH environment variable:
nodejs
directory is most likely under:C:\Program Files\nodejs
C:\Program Files (x86)\nodejs
node.exe
(executable) file.Next, ensure you also have the path to your npm directory in the PATH as well.
Your npm
directory is most likely located under
%USERPROFILE%\AppData\Roaming\npm
, which is the same as
C:\Users\YOUR_USER\AppData\Roaming\npm
.
%USERPROFILE%\AppData\Roaming\npm # ๐๏ธ same as below (make sure to replace YOUR_USER) C:\Users\YOUR_USER\AppData\Roaming\npm
If you can't find it, try running the npm config get prefix
command.
npm config get prefix
Add the path to npm
and click on "OK" twice to confirm.
Close your Command prompt application and then reopen it.
You might also have to restart your PC, but that's not always necessary.
You can start a new CMD shell and use the npx --version
command to make sure
npx
is installed and configured properly.
npx --version
If you got the error in Visual Studio Code and restarting VSCode doesn't help, try to click on the trash bin button to restart the terminal.
If the error persists, try to run CMD or VSCode as an administrator before
issuing an npx
command.
To open CMD as an administrator:
Click on the Search bar and type CMD.
Right-click on the Command Prompt application and click "Run as administrator".
You can run VSCode as an administrator by right-clicking on VSCode and clicking "Run as administrator".
Try to issue an npx
command with elevated permissions, e.g. npx --version
.
If the error persists, try running the following command to install npx
.
# ๐๏ธ for Windows npm install -g npx # ๐๏ธ for macOS or Linux sudo npm install -g npx
To solve the error "'npx' is not recognized as an internal or external command, operable program or batch file", make sure:
You can learn more about the related topics by checking out the following tutorials: