npx: command not found Error in Windows, Linux, macOS [Fix]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
6 min

banner

# Table of Contents

  1. npx: command not found error on macOS and Linux
  2. 'npx' is not recognized as an internal or external command

Note: if you got the error 'npx' is not recognized as an internal or external command on Windows, click on the second subheading.

# npx: command not found error on macOS and Linux

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.

shell
npm install -g npx

install npx macos linux

If you get a permissions error, prefix the command with sudo.

shell
sudo npm install -g npx

sudo install npx

Note: if you got the error 'npx' is not recognized as an internal or external command on Windows, click on the following subheading.

# Update your PATH environment variable on macOS or Linux

If the error persists, run the following command.

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

~/.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.

Install npx by running the following command.

shell
npm install -g npx

install npx macos linux

If you get a permissions error, prefix the command with sudo.

shell
sudo npm install -g npx

sudo install 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.

# 'npx' is not recognized as an internal or external command

The error "'npx' is not recognized as an internal or external command, operable program or batch file" occurs for 2 main reasons:

  1. Not having Node.js installed on your machine.
  2. Not having Node in your system's PATH environment variable.

npx is not recognized as internal or external command

Depending on your operating system, the error message might be:

  • 'npx' is not recognized as an internal or external command, operable program or batch file
  • The term 'npx' is not recognized as the name of a cmdlet, function, script file, or operable program.
  • 'npx': command not found

If you have Node.js installed and added to your PATH environment variable, use the following command to install npx.

cmd
# ๐Ÿ‘‡๏ธ for Windows npm install -g npx # ๐Ÿ‘‡๏ธ for macOS or Linux sudo npm install -g npx

install npx

If you get a permissions error when trying to install npx, run CMD as an administrator and rerun the command.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

You can issue the npx --version command to check if npx is installed.

cmd
npx --version

get npx version

If that didn't help, run the Node.js installer and add npm to your PATH environment variable.

To install Node and add it to your PATH environment variable:

  1. Open the nodejs.org page and download the Windows installer for the LTS (long-term supported) version.

download node lts version

  1. Start the installer and click Next on the Welcome screen.

node start installer

  1. Accept the end-user license agreement and click Next.

node accept end user agreement

  1. Leave the default destination folder selected and click Next.

node select destination folder

  1. On the "Custom Setup" screen, click Next. Notice that there is an "Add to PATH" option selected.

node custom setup

  1. You can optionally install tools for native modules, otherwise, click Next.

node tools for native modules

  1. On the next screen, click on the Install button.

node click install

  1. Lastly, click on the Finish button.

node click finish

  1. Make sure to close any existing command prompts and PowerShell instances for the PATH variable to get updated.

  2. You can start a new CMD shell and use the npx --version command to make sure npx is installed.

cmd
npx --version node --version

get npx version

It is very important that you close and reopen CMD or PowerShell before issuing any 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.

restart vscode terminal

If the error persists, add Node to your PATH environment variable manually.

# Add Node to your PATH environment variable on Windows

To add Node to your PATH environment variable:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

click environment variables

  1. In the "System variables" section, select the "Path" variable and click "Edit".

select path and click edit

  1. Click on "New" and then click "Browse".

click new browse

  1. Your nodejs directory is most likely under:
  • C:\Program Files\nodejs
  • or C:\Program Files (x86)\nodejs
We are looking for the directory that contains the node.exe (executable) file.

locate node exe directory

  1. Next, ensure you also have the path to your npm directory in the PATH as well.

  2. Your npm directory is most likely located under %USERPROFILE%\AppData\Roaming\npm, which is the same as C:\Users\YOUR_USER\AppData\Roaming\npm.

location
%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.

cmd
npm config get prefix

npm config get prefix

  1. Add the path to npm and click on "OK" twice to confirm.

  2. Close your Command prompt application and then reopen it.

Note that you must restart your Command prompt shell for the changes to take effect.

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.

cmd
npx --version

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

restart vscode 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:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd 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.

cmd
# ๐Ÿ‘‡๏ธ for Windows npm install -g npx # ๐Ÿ‘‡๏ธ for macOS or Linux sudo npm install -g npx

install npx

# Conclusion

To solve the error "'npx' is not recognized as an internal or external command, operable program or batch file", make sure:

  1. You have Node.js installed on your machine.
  2. You have Node in your system's PATH environment variable.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev