user2447562
user2447562

Reputation: 3641

Npm install cannot find module 'semver'

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)

Upvotes: 350

Views: 440090

Answers (30)

artiebits
artiebits

Reputation: 5195

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

Upvotes: 463

Puser
Puser

Reputation: 149

  1. Delete node_modules folder

  2. Restart trminal

  3. Run npm whatever

Upvotes: 0

Mahedi Hasan
Mahedi Hasan

Reputation: 1078

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

Upvotes: 12

Deven T. Corzine
Deven T. Corzine

Reputation: 607

I encountered this Cannot find module 'semver' error while running npm install in WSL1 under Windows 10, with Ubuntu 22.04.2 LRS (jammy) as the Linux installation.

Using strace to see what syscalls were made, I found that Node.js was looking for /usr/share/nodejs/npm/node_modules/semver/package.json, which didn't exist. However, that module does appear to exist at the top level, in /usr/share/nodejs/semver/package.json. After some experimentation, I found that adding the top-level directory to Node's search path appears to fix the problem. This worked for me:

NODE_PATH=/usr/share/nodejs npm install

Therefore, I've added the environment variable setting to my ~/.bashrc file:

export NODE_PATH=/usr/share/nodejs

Upvotes: 0

Nati Kamusher
Nati Kamusher

Reputation: 771

If you happen to get a 'semver' typescript error for the 'import' keyword, check if your file's extension is js instead of ts

Upvotes: 0

Mohamed Farouk
Mohamed Farouk

Reputation: 1229

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

Upvotes: 13

Vineet Sajwan
Vineet Sajwan

Reputation: 196

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
  3. Install node
    • nvm install e.g. nvm install 8.16
    • Above command will itself install node js and its compatible NPM.

Upvotes: 4

Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

On WSL (so, ~Ubuntu), I faced this problem after having installed both, nodejs and npm through apt, then updated Node using n.

I was able to resolve it by completely removing apt's installation of npm: sudo apt purge npm.

Upvotes: 2

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

Upvotes: 2

roni naruka
roni naruka

Reputation: 79

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

Upvotes: 4

Jacob Solawetz
Jacob Solawetz

Reputation: 378

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

Upvotes: 2

林果皞
林果皞

Reputation: 7813

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.

Upvotes: 3

Ivan  Silkin
Ivan Silkin

Reputation: 485

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

Upvotes: 2

darylvickerman
darylvickerman

Reputation: 622

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.

Upvotes: 38

Geoff Langenderfer
Geoff Langenderfer

Reputation: 946

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

Upvotes: 6

Rashid Iqbal
Rashid Iqbal

Reputation: 1271

just remove your node_modules

rm -rf node_modules

than

yarn

or

npm install

Upvotes: 3

Justin Joseph
Justin Joseph

Reputation: 3899

I got same error and I solved it.

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

Upvotes: 6

Grant
Grant

Reputation: 6349

Start fresh.

brew uninstall --force node
brew install node

Upvotes: 2

Jaden Goodwin
Jaden Goodwin

Reputation: 61

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.

Upvotes: 2

Snowcat
Snowcat

Reputation: 526

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.

Upvotes: 5

Schmudde
Schmudde

Reputation: 1178

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.

Upvotes: 65

Supun Madushanka
Supun Madushanka

Reputation: 3238

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

Upvotes: 285

master_dodo
master_dodo

Reputation: 1347

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.

Upvotes: 1

the_haystacker
the_haystacker

Reputation: 1625

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

yarn global add npm

Upvotes: 7

zeeawan
zeeawan

Reputation: 6905

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

Upvotes: 9

J&#233;r&#244;me Beau
J&#233;r&#244;me Beau

Reputation: 11450

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

Upvotes: 7

Shubham Jaiswal
Shubham Jaiswal

Reputation: 610

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.

Upvotes: 13

sachin
sachin

Reputation: 250

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

Upvotes: 0

Jerinaw
Jerinaw

Reputation: 5529

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.

Upvotes: 0

Ashoor
Ashoor

Reputation: 1368

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!

Upvotes: 109

Related Questions