4

My npm has worked well, however today it shows an error saying,

Error: Cannot find module 'C:\Users\mrsim\node_modules\npm\bin\npm-cli.js'

My npm worked well just an hour ago. I typed "npm start" inside of VSCode to start my react-app and it worked very well!

This nightmare started when I created server folder and typed "npm install express".

Normally when I typed "npm install express" in the terminal, it automatically created node-modules folder and package.json for me without any problem, but none of these appear.

To solve this problem I repair node first, and didn't worked.

Next I changed my environment variable, and didn't worked.

Next I used "SET PATH=C:\Program Files\Nodejs;%PATH%" this sentence in the terminal, and didn't worked.

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'C:\Users\mrsim\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'C:\Users\mrsim\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

This is error message appear on terminal. If i go to that directory, i cannot find npm.cli.js

What should I do...?

6
  • Any npm command returns the same error ? Have you tried uninstall express ? Commented Mar 15, 2022 at 13:35
  • everytime i type any npm command in terminal, always same error occur. And that's why i cannot uninstall express, since it is also npm command Commented Mar 15, 2022 at 14:08
  • i deleted, reinstalled node and still it didn't work, same error occur. Commented Mar 15, 2022 at 14:11
  • Have you tried to delete that node_modules directory in your home (C:\Users\mrsim\node_modules) ? Commented Mar 15, 2022 at 14:49
  • 1
    Thanks for your advice! I'll try it :) have a nice day! Commented Mar 15, 2022 at 15:26

8 Answers 8

5
  1. Go to the path that have Appdata\local or Appdata\roaming
  2. Delete the npm and npm cache folder in both
  3. Get back in terminal, Run npm install again, will see a new path ex: npm notice
  4. Run npm install -g [email protected] to update!
  5. Install the new path and ready to go

This solved my problems.

Sign up to request clarification or add additional context in comments.

1 Comment

The above steps given, helped me to resolve the error. Thanks!!!
1

uninstall node.

then go to;

your programfiles folder and delete nodejs folder.

then install node again.

This work for me.

Comments

0

One of the root cause of this issue while using node version managers along with the NodeJS native installer. Which is not recommended.

The way of installing NodeJS in a developer machine is with the version managers. Check here to find out the recommended version managers here https://nodejs.dev/en/

For a temp fix for the above issue, update the path variable pointing the npm installation or reinstall nodejs.

Comments

0

On windows 11, I could solve this issue by uninstalling node.js and removing the chocolatey directories: "C:\ProgramData\chocolatey" and "C:\ProgramData\ChocolateyHttpCache" then reinstalling node.js with the installer (https://nodejs.org/en/download).

Comments

0

For those who installed node by nvm like me

open Control Panel -> Programs -> Program and Features -> uninstall Node and NVM (if you have it or install node by nvm)

Go to your Browser and install Node by these commands in your Powershell:

# installs fnm (Fast Node Manager)
winget install Schniz.fnm

# configure fnm environment
fnm env --use-on-cd | Out-String | Invoke-Expression

# download and install Node.js
fnm use --install-if-missing 20

# verifies the right Node.js version is in the environment
node -v # should print `v20.17.0`

# verifies the right npm version is in the environment
npm -v # should print `10.8.2` 

ONLY IF IT CAN NOT FIND fnm

1.Open Your PowerShell Profile You need to edit your PowerShell profile script. To do this, run the following command in PowerShell:

notepad $PROFILE

If the file doesn’t exist, PowerShell will prompt you to create it. Click "Yes."

Step 2: Add the fnm Environment Configuration In the profile script that opens in Notepad, add the following lines:

# Load fnm and set it to use-on-cd
fnm env --use-on-cd | Out-String | Invoke-Expression

This ensures that every time you open a new PowerShell window, fnm is initialized with the correct environment settings.

Step 3: Save and Close Save the changes in Notepad and close it.

Last: Check your node and npm version by

node -v
npm -v

Comments

0

In my situation I simply ran npx commands from the terminal as Administrator, this was a more correct way for npm to access the root node_modules.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Don't worry, just paste this on your terminal, and you're good to go!

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

Actual credits: @ammarsecurity

1 Comment

This works.Thank you.
-1

Okay, it seems like you installed the npm module in a wrong directory. To fix, this follow the following steps,

  1. Open File Explorer and go in this directory,
C:\Users\mrsim\
  1. You will see a folder named "node_modules". Delete it completely.
  2. Now open up your command prompt
  3. Copy your project directory and paste the following:-
cd <project_dir>
  1. Run,
npm install express
  1. Now it should be installed in the correct directory!

1 Comment

thanks for your advice! However I tried it and still same error occurs... I guess i need to reset my labtop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.