admin管理员组

文章数量:1125713

I can't use npm install using the command prompt in NodeJS. I'm getting these errors when running npm install:

module.js:339
    throw err;
    ^
Error: Cannot find module 'semver'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\npm\l
ib\config\defaults.js:6:14)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)

I can't use npm install using the command prompt in NodeJS. I'm getting these errors when running npm install:

module.js:339
    throw err;
    ^
Error: Cannot find module 'semver'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\npm\l
ib\config\defaults.js:6:14)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
Share Improve this question edited May 9, 2020 at 10:54 Kalle Richter 8,66528 gold badges91 silver badges203 bronze badges asked Nov 23, 2015 at 11:55 user2447562user2447562 3,6312 gold badges18 silver badges15 bronze badges 7
  • 5 I had the same problem Alternatively, you can use yarn, instead of npm – Wojciech Maślanka Commented Dec 5, 2016 at 14:00
  • 2 Are you using yarn to install the package.json ? – anandharshan Commented Mar 15, 2017 at 14:49
  • 1 Just reinstall npm – Nuno Ferro Commented Feb 21, 2018 at 11:59
  • 4 I stumbled upon the issue when I upgraded node in my ubuntu and closed terminal. But I forgot to close and open Visual Studio again. Once reopened, the issue went away. :) – Sangam Uprety Commented Jul 15, 2021 at 7:58
  • 1 Similar as @sangam: I just restarted Linux, and then it worked. – Ralf Commented Mar 18, 2022 at 14:38
 |  Show 2 more comments

42 Answers 42

Reset to default 1 2 Next 463

Deleting other global modules solved this issue for me:

  • sudo rm -rf /usr/local/lib/node_modules
  • sudo rm -rf ~/.npm
  • brew uninstall --force node
  • brew install node

I had the same error and npm uninstall npm -g, rm -rf node_modules didn't help me, because when I tried I was getting Error: Cannot find module 'semver'.

if you are in Linux server exit from the server and re ssh

I'm facing the same issue here.

If this occurs right after you run brew install yarn try running yarn global add npm and voilà - fixed!

On MS Windows, the solution is to remove %APPDATA%\npm and reinstall node

On Ubuntu, if you would rather manage Node through the default repositories using apt, it's best to install both nodejs and npm this way.

It's very likely that the version of npm you are using is expecting semver in a specific place that is not aligned with the version of Node JS you have installed. If there is only one version of nodejs and only one version of npm, and they are both installed using the default repositories, this should work perfectly.

Here is my best guidance for getting Node and npm working using the main Ubuntu repositories:

  1. sudo apt-get purge nodejs --auto-remove and sudo apt-get purge npm --auto-remove
  2. whereis node: remove all the versions of node, one at a time. Repeat until this command returns a blank after node:.
  3. sudo apt-get install nodejs
  4. node --version: should return ... no such file or directory
  5. nodejs --version: should return v1x.x.x. This is the version installed by apt. It will probably be less recent that the latest version available on the Node JS website.
  6. sudo apt-get install npm
  7. npm --version: it will use the nodejs installed by apt and work correctly.

I had this too, after running brew install yarn yesterday. At least, everything was fine up until then.

I ran rm -rf node_modules and tried to reinstall, but no npm command was working.

In the end I took the rather simple step of reinstalling Node via the official Node installer for Mac OS X.

https://nodejs.org/en/download/

Everything is fine now. Just went back to the directory, ran npm install and it's done the trick.

I had the same problem, the way i solved it was by executing

PATH="$PATH"

in terminal, problem was the terminal kept the old path of nodejs instead of using the new one.

On Arch Linux what did the trick for me was:

sudo pacman -Rs npm
sudo pacman -S npm

Having just encountered this on Arch Linux 4.13.3, I solved the issue by simply reinstalling semver:

pacman -S semver

Actually, it is taking the reference of previously stored modules.

Solution:Delete the npm-cache/npm folder in the installation directory of nodejs. In windows, it is in C:/User/Username/Appdata/Roaming/npm(or npm_cache). and try to install again.

Same thing to do in any OS.

sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* ~/.npm
sudo rm -rf /usr/local/lib/node*
sudo rm -rf /usr/local/bin/node*
sudo rm -rf /usr/local/include/node*

sudo apt-get purge nodejs npm
sudo apt autoremove

Then install node and npm

original answer

Just uninstall node js then reinstall it using nvm. Your problem will be solved. I am a debian 10 user.

To install nvm, follow the instructions in the latest nvm installation script or run the following (potentially out of date) command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

after executing the command. close your terminal and reopen then run this command to install node js latest version.

nvm install --lts

now check the node version by running node -v

In my case on macOS(10.13.6), when I executed the following command

npm install -g react-native-cli

I got this error

Error: Cannot find module 'semver'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

The error got resolved after executing the command

yarn global add npm

proposed by @Ashoor

Just make sure semver is installed: npm install -g semver

For me, this happened after I installed yarn globally. To resolve this issue, install npm using yarn and done.

yarn global add npm

I got same error and I solved it.

delete package-lock.json file and node_modules folder then npm install

For arch linux

sudo pacman -Rsc -n nodejs
sudo pacman -Sy nodejs
sudo pacman -Sy npm

Found here: https://github.com/npm/npm/issues/15558

On Windows, downloading Node's MSI again and doing a 'Repair' worked for me.

I had the same issue but it was caused by a broken package-lock.json file.

Deleting package-lock.json and running npm install again fixed it for me.

This worked for me on Ubuntu (latest version dated Oct/2020)

I had to first get code from the bash source:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

This got the latest version of node which updated my libraries and got rid of the 'semver' error.

First you can try close current terminal tab and open new its work fine

This issue usually comes when node js and npm versions are not compatible. To fix this in Ubuntu (my version was 20)

  1. Uninstall node js and npm.
    • apt remove npm
    • apt remove nodejs
    • which node (it will return node js files path)
    • Delete node directory
  2. Install NVM
    • Installation
  3. Install node
    • nvm install e.g. nvm install 8.16
    • Above command will itself install node js and its compatible NPM.

just remove your node_modules

rm -rf node_modules

than

yarn

or

npm install

Just a reminder of executable npm execute via node env:

xb@dnxb:/tmp$ type -a npm
npm is /usr/bin/npm
npm is /bin/npm
xb@dnxb:/tmp$ realpath /usr/bin/npm /bin/npm
/usr/share/npm/bin/npm-cli.js
/usr/share/npm/bin/npm-cli.js
xb@dnxb:/tmp$ 
xb@dnxb:/tmp$ head -1 /usr/share/npm/bin/npm-cli.js
#!/usr/bin/env node

I can check which node is chosen by type -a, the first line has the most priority, i.e. I can get same error if I run /usr/local/bin/node /usr/share/npm/bin/npm-cli.js, but no error if I run /usr/bin/node /usr/share/npm/bin/npm-cli.js:

xb@dnxb:/tmp$ type -a node
node is /usr/local/bin/node
node is /usr/bin/node
node is /bin/node
xb@dnxb:/tmp$ realpath /usr/local/bin/node
/usr/local/bin/node
xb@dnxb:/tmp$ realpath /usr/bin/node
/usr/bin/node
xb@dnxb:/tmp$ realpath /bin/node
/usr/bin/node
xb@dnxb:/tmp$ 

There are 2 unique node exe based on realpath. If you have the similar case, then you need to find out how to remove the undesired/broken node exe, e.g. sudo rm /usr/local/bin/node or manipulate env $PATH.

Faced this issue when I ran npm install using a lower version of node. Then later when I upgraded to latest version of node and ran gulp, it has shown this error.

To resolve, deleted node_modules and re-ran npm install. Then gulp ran fine.

i was getting an error saying Permission Denied after running any 'ng' command (ng --version). I googled for a while and tried clearing npm cache npm cache verify, uninstalling my global angular cli (npm uninstall -g @angular/cli) and reinstalling Angular/cli (npm install -g @angular/cli) etc.. but it would give an error say its already installed. but the node_modules folder here wouldn't have any angular folder.. reinstalled node even then restarted my computer.

ANSWER: Finally I found that the ng.cmd and ng.ps1 files in C:\Users\JaGoodwin\AppData\Roaming\npm\ here were still there (in npm folder).. even though I did npm uninstall -g @angular/cli. those files were causing ng (angular/cli) to think it was still installed. i deleted those files then npm install -g @angular/[email protected] (version i need) I then removed my projects node_modules and then ran npm install and now can run my angular project using ng serve.

C:\Users\JaGoodwin\AppData\Roaming\npm\

Find this by folder searching %APPDATA% in your windows search bar.

Start fresh.

brew uninstall --force node
brew install node

Just delete (or rename) the cloned repository's version of semver folder: "gitClonedRepository/App/node_modules/semver"

and copy the "semver" folder from "C:\Program Files\nodejs\node_modules\npm\node_modules\semver"

into the application's node_modules folder.

(if you're on Windows, othervise it must be located somewhere in /usr/local/lib/node_modules).

All I needed was sudo rm -rf node_modules/ in the directory I was working in. This happened after I updated my node version with n lts

Nevermind install Yarn.Performs parallel installation resulting in better speed and performance from npm

 sudo apt remove yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - 
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update
sudo apt-get install yarn -y

本文标签: javascriptNpm install cannot find module 39semver39Stack Overflow