Npm Cannot find module npm-cli.js error [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# Npm Cannot find module npm-cli.js error [Solved]

The error "Cannot find module npm-cli.js" occurs when the path to Node.js in your system's PATH environment variable is incorrect.

To solve the error, repair the Node.js application and correct the path.

shell
internal/modules/cjs/loader.js:584 throw err; ^ Error: Cannot find module 'C:\Users\Bobbyhadz\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15) at Function.Module._load (internal/modules/cjs/loader.js:508:25) at Function.Module.runMain (internal/modules/cjs/loader.js:754:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

The first thing you should try is to repair Node.js:

  1. Click on the Search bar and type "Apps & features" and start the application.

type apps and features in search bar

  1. Click on "Programs and Features".

click programs and features

  1. Find the Node.js application, right-click on it and click "Repair".

find node js and click repair

  1. Restart your terminal and your IDE and try to issue the npm --version command to see if the error is resolved.

If the error is not resolved, try to issue the following command in CMD (Command Prompt).

cmd
SET PATH=C:\Program Files\Nodejs;%PATH%

If the error persists, you have to correct the path to Node.js in your system's PATH environment variable.

I'll go through all the steps of installing Node.js and setting the path to Node in your system's path environment variable.

If you already have Node.js installed correctly, scroll down to the next subheading.

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) 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 VS Code 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.

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