'npm' is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

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

The error "'npm' 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.

npm is not recognized as internal or external command

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 npm --version command to make sure Node is installed.

cmd
npm --version node --version

get node npm version

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

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 with your actual username) 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 npm --version command to make sure Node is installed and the npm command works.

cmd
npm --version node --version

get node npm 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 npm 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 npm command with elevated permissions, e.g. npm --version.

# Conclusion

To solve the error "'npm' 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.