26

I am getting the following error whenever I try to run npm command.

internal/modules/cjs/loader.js:626
throw err;
^

Error: Cannot find module 'semver'
Require stack:
- /usr/share/npm/lib/utils/unsupported.js
- /usr/share/npm/bin/npm-cli.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)
    at Function.Module._load (internal/modules/cjs/loader.js:527:27)
    at Module.require (internal/modules/cjs/loader.js:681:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/usr/share/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Module.load (internal/modules/cjs/loader.js:641:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:681:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/share/npm/lib/utils/unsupported.js',
    '/usr/share/npm/bin/npm-cli.js'
  ]
}

I was basically trying to make offline use of FreeCodeCamp. It asked me to run command 'npm ci' from which I started getting this error.

I tried reinstalling npm but it won't work. I also tried installing the semver module which also didn't help.

What might have gone wrong and how can I fix this?

8 Answers 8

68

Found a similar question here and the answer worked.

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

Download the latest tar.xz NodeJS file from https://nodejs.org/en/download/

tar -xf node-v#.#.#-linux-x64.tar.xz
sudo mv node-v#.#.#-linux-x64/bin/* /usr/local/bin/
sudo mv node-v#.#.#-linux-x64/lib/node_modules/ /usr/local/lib/

Where #.#.# is the version you downloaded.

Verify installation using

node -v
npm -v
3
  • doesn't work for me on Ubuntu 20, module still missing
    – Gediminas
    Dec 6, 2021 at 16:03
  • this has node, but not npm
    – IceFire
    Aug 7, 2022 at 11:36
  • 1
    Worked for me on Ubuntu 22.04. Thank you! Mar 2 at 7:24
6

It depends on how you want to manage Node JS/npm.

As Bilbo Baggins suggested, you can install the latest LTS version of Node JS from the official website: https://nodejs.org/en/download/.

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

I was using nvm to manage node versions, In my case npm syslink was broken i solved it by uninstalling and then reinstalling node again

check your node version using node --version

then run nvm uninstall <your version>

then reinstall using nvm install <your version

3

Run this command it will automatically remove previous version of npm and install new version of npm, worked for me, you can check here for details https://github.com/npm/cli

 curl -qL https://www.npmjs.com/install.sh | sh
2
  • This worked for me but I had to run it as root.
    – Iftieaq
    Jun 15, 2021 at 18:43
  • Yes you have to run via sudo Jun 24, 2021 at 16:41
3

I had the same issue, but on 22.04. None of the above worked for me, but I found this process that did. https://codemarvels.in/2021/03/20/npm-install-gives-error-cannot-find-module-semver/ Essentially:

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
sudo apt-get install nodejs npm
1
  • works for my ubuntu 22
    – p1gd0g
    Apr 13, 2023 at 9:36
1

I was facing an Angular project. Somehow, Angular-CLI (ng) was removed.
So I just verified that node and npm are installed and did the following which fixed it:

sudo npm install -g @angular/cli 
0
1

In my case I was using nvm so I had to remove all node versions (rm -rf ~/.nvm/versions/node/*) then install both nodejs and npm :

sudo apt install nodejs
sudo apt install npm
0

The following command resolved problem on my Ubuntu 22.04 PC.

hash -r

n command said

If "node --version" shows the old version then start a new shell, or reset the location hash with:
hash -r  (for bash, zsh, ash, dash, and ksh)
rehash   (for csh and tcsh)

The command I install latest version of npm to Ubuntu22.04.

sudo apt install -y npm
sudo npm install -g n
sudo n stable
hash -r
npm --version

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .