Npm install cannot find module 'semver'

Javascriptnode.jsMean Stack

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

I had the same error. npm uninstall npm -g, rm -rf node_modules didn't help me, because when I tried I was getting Error: Cannot find module 'semver'. But I solve my problem with these steps (this will delete other global modules you may be using):

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

Hope this will help those who are getting a similar problem.

Solution 2 - Javascript

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!

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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.

Solution 7 - Javascript

On Arch Linux what did the trick for me was:

sudo pacman -Rs npm
sudo pacman -S npm

Solution 8 - Javascript

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

pacman -S semver

Solution 9 - Javascript

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.

Solution 10 - Javascript

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.

Solution 11 - Javascript

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

Solution 12 - Javascript

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

yarn global add npm

Solution 13 - Javascript

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

Solution 14 - Javascript

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.

Solution 15 - Javascript

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

Solution 16 - Javascript

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

Solution 17 - Javascript

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

mahedi@debain:~$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

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

mahedi@debain:~$ nvm install v14.15.5

now check by typing node -v

Solution 18 - Javascript

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.

Solution 19 - Javascript

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

Solution 20 - Javascript

I got same error and I solved it.

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

Solution 21 - Javascript

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.

Solution 22 - Javascript

Just remove the /node_modules folder and try again. ;)

You can use npm rm to remove it.

You can also check this issue on GitHub. To be more specific about unnistal, try to follow this:

> Uninstalling > > So sad to see you go. > > sudo npm uninstall npm -g Or, if that fails, > > sudo make uninstall More Severe Uninstalling > > Usually, the above instructions are sufficient. That will remove npm, > but leave behind anything you've installed. > > If you would like to remove all the packages that you have installed, > then you can use the npm ls command to find them, and then npm rm to > remove them. > > To remove cruft left behind by npm 0.x, you can use the included > clean-old.sh script file. You can run it conveniently like this: > > npm explore npm -g -- sh scripts/clean-old.sh npm uses two > configuration files, one for per-user configs, and another for global > (every-user) configs. You can view them by doing: > > npm config get userconfig # defaults to ~/.npmrc npm config get > globalconfig # defaults to /usr/local/etc/npmrc Uninstalling npm does > not remove configuration files by default. You must remove them > yourself manually if you want them gone. Note that this means that > future npm installs will not remember the settings that you have > chosen.

Solution 23 - Javascript

I finally figured this out! Yes, you have to uninstall/delete all files and reinstall. BUT SOME OF THE FILE PATHS ARE TOO LONG TO DELETE DURING THE UNINSTALL PROCESS. The file paths are so long in Node that they may not be able to be deleted. You have to rename the file paths.

After uninstalling Node, use Command Prompt, navigate to ...\Appdata\roaming\ (use the path from the error message). Try to delete the folder with del npm. Then, if it doesn't allow you to delete a folder, start renaming the folders in the file path in the next error message to "a" or something short. You may have to do this for multiple folders along the path. Then you should be able to delete! Then reinstall node and it should work.

Solution 24 - Javascript

just remove your node_modules

rm -rf node_modules

than

yarn

or

npm install

Solution 25 - Javascript

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.

Solution 26 - Javascript

In my case, simply re-running brew install yarn fixed the problem.

Solution 27 - Javascript

I faced with same trouble after brew installs yarn on my MacOs Sierra 10.12.6. (And the first try of installation fall after brew update self with a message " Homebrew must be run under Ruby 2.3! You're running 2.0.0. (RuntimeError)". So I re-run it.)

So I use n manager to reinstall npm.

  • switch to another node version (for my case to 8.0) by "n" command with empty parameters
  • or install new by "n x.x.x" where x.x.x the node version number (i need 6.9.1)
  • delete working version by "n rm x.x.x" where x.x.x the node version number. use sudo if need
  • install working version by "n x.x.x" where x.x.x the node version number. use sudo if need
  • switch by to the working version of node. (see first step for reference)

I think the trouble can be caused that node was installed under sudo for case.

Solution 28 - Javascript

I had the same issue installing on AWS Linux. I had to install it with sudo. So to get around this I followed step 3 from this article (making sure to get the latest version of node)

https://www.hostingadvice.com/how-to/update-node-js-latest-version/

wget https://nodejs.org/dist/vx.x.x/node-vx.x.x-linux-x64.tar.xz
tar -C /home/aUser/node --strip-components 1 -xJf node-vx.x.x-linux.x64.tar.xz

But installed it to the user's home directory /home/aUser/node. Then added that path to my PATH.

export PATH=/home/aUser/node/bin:$PATH

After that I was able to do an npm install with no issues.

Solution 29 - Javascript

I faced this issues while running project on higher version of cli. To solve this issue you can simply comment out @angular-devkit/build-angular module and again you have to reinstall it through

> npm install @angular-devkit/build-angular --save

Solution 30 - Javascript

For me, it happened while upgrading to gulp 4 from gulp 3 in my project. So, when I run "gulp watch" command, it was throwing me same error.

It's probably due to version mismatch as compared to my globally installed gulp.

So, either upgrading gulp with "-g" global flag would help, or else I followed this below

However, I used "./node_modules/.bin/gulp watch" in my project. This ensures that all the dependencies that gulp needs are taken from node_modules only.

Solution 31 - Javascript

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.

enter image description here

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

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

Solution 32 - Javascript

Start fresh.

brew uninstall --force node
brew install node

Solution 33 - Javascript

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

Solution 34 - Javascript

Just check your preinstall scripts if you have one. Sometimes to restrict the versions of node and npm one needs to run a project.

If that's the case you need to install semver manually via npm install -g semver

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2447562View Question on Stackoverflow
Solution 1 - JavascriptartiebitsView Answer on Stackoverflow
Solution 2 - JavascriptAshoorView Answer on Stackoverflow
Solution 3 - JavascriptSupun MadushankaView Answer on Stackoverflow
Solution 4 - JavascriptGangadharView Answer on Stackoverflow
Solution 5 - JavascriptcodewithfeelingView Answer on Stackoverflow
Solution 6 - JavascriptSchmuddeView Answer on Stackoverflow
Solution 7 - JavascriptTelmo TrooperView Answer on Stackoverflow
Solution 8 - JavascriptMatthias BraunView Answer on Stackoverflow
Solution 9 - JavascriptdarylvickermanView Answer on Stackoverflow
Solution 10 - JavascriptShubham JaiswalView Answer on Stackoverflow
Solution 11 - JavascriptzeeawanView Answer on Stackoverflow
Solution 12 - Javascriptthe_haystackerView Answer on Stackoverflow
Solution 13 - JavascriptSornakumarView Answer on Stackoverflow
Solution 14 - JavascriptWireblueView Answer on Stackoverflow
Solution 15 - JavascriptJavaromeView Answer on Stackoverflow
Solution 16 - JavascriptMohamed FaroukView Answer on Stackoverflow
Solution 17 - JavascriptMahedi Hasan DurjoyView Answer on Stackoverflow
Solution 18 - JavascriptSnowcatView Answer on Stackoverflow
Solution 19 - JavascriptGeoff LangenderferView Answer on Stackoverflow
Solution 20 - JavascriptJustin JosephView Answer on Stackoverflow
Solution 21 - JavascriptcedricView Answer on Stackoverflow
Solution 22 - JavascriptRodmentouView Answer on Stackoverflow
Solution 23 - JavascriptThe WindhoverView Answer on Stackoverflow
Solution 24 - JavascriptRashid IqbalView Answer on Stackoverflow
Solution 25 - Javascript林果皞View Answer on Stackoverflow
Solution 26 - Javascriptjack4itView Answer on Stackoverflow
Solution 27 - JavascriptLarestView Answer on Stackoverflow
Solution 28 - JavascriptJerinawView Answer on Stackoverflow
Solution 29 - Javascriptsachin View Answer on Stackoverflow
Solution 30 - Javascriptmaster_dodoView Answer on Stackoverflow
Solution 31 - JavascriptJaden GoodwinView Answer on Stackoverflow
Solution 32 - JavascriptGrantView Answer on Stackoverflow
Solution 33 - JavascriptIvan SilkinView Answer on Stackoverflow
Solution 34 - JavascriptMr94View Answer on Stackoverflow