Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm cannot find module 'semver' after reinstall

I am using ubuntu 19.04.

I am getting following error, when trying to do anything with npm

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

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:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/usr/share/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789: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)

I have tried to apply different solutions from SO, but unfortunately they have not worked.

It started happening after reinstalling NPM because of some errors I unfortunately cannot recreate.

What I have tried

sudo apt purge nodejs
sudo apt autoremove

after these two steps, npm is uninstalled.

npm -v
bash: /usr/bin/npm: No such file or directory

I proceed to

sudo apt install nodejs

However the error occurs even after I have purged the files

User@User:/$ node -v
v10.16.3
User@User:/$ npm -v
internal/modules/cjs/loader.js:638
    throw err;
    ^

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:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/usr/share/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789: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)

I have also tried following

sudo apt purge npm
sudo apt purge nodejs
sudo apt autoremove
sudo apt install npm
sudo apt install nodejs
sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf ~/.npm
brew uninstall --force node
brew install node

It still yields the same error. The global files should be uninstalled with both of these approaches.

To be certain I have searched the computer with and deleted anything if it looked like something relevant to node or npm.

sudo find / -type f -name "node"
sudo find / -type f -name "node_modules"
sudo find / -type f -name "npm"

It is if something is carried on through the purging, perhaps some files communication with NPM yielding the error.

like image 862
jonasgroenbek Avatar asked Sep 16 '19 13:09

jonasgroenbek


People also ask

What is SemVer in npm?

SemVer stands for Semantic Versioning. NPM stands for Node Package Manager. Because there can be different versions of a module that is installable, and the need to install various versions can differ, there exists what we call SemVer. NPM is used to install packages or modules in NodeJS.

What is SemVer in node JS?

Semantic Versioning, otherwise known as semver has become a core part of Node. js software development. Thanks to npm, semver is embedded in the way we publish and link packages together to form simple libraries or complex applications.

How do I download Npmjs packages?

If you haven't installed npm, with the current public API, you can also access the information about a package in the npm registry from the URL https://registry.npmjs.org/<package-name>/ . Then you can navigate the JSON at versions > (version number) > dist > tarball to get the URL of the code archive and download it.


2 Answers

Try

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
apt-get update
apt-get install nodejs

You might also want to check out the answers here: Npm install cannot find module 'semver'

like image 198
Felix G Avatar answered Oct 11 '22 03:10

Felix G


I had a similar issue and reinstalling works for me. this straightforward approach works fine for me. founded here

First, remove old staff

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, Download the latest tar.xz NodeJS file from official site

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.

finally, Verify installation using

node -v
npm -v
like image 44
Omar Avatar answered Oct 11 '22 03:10

Omar