3
$ npm install

internal/modules/cjs/loader.js:1023
  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:1020:15)
    at Function.Module._load (internal/modules/cjs/loader.js:890:27)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/usr/share/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Module.require (internal/modules/cjs/loader.js:1080:19) {

  code: 'MODULE_NOT_FOUND',

  requireStack: [
    '/usr/share/npm/lib/utils/unsupported.js',
    '/usr/share/npm/bin/npm-cli.js'
  ]
}
Kevin Bowen
  • 20,055
  • 57
  • 82
  • 84
MrPina
  • 177

2 Answers2

1

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.
Schmudde
  • 181
0

You probably just don't have the semver module.
Ubuntu might be using some old nodejs version & it doesn't have it.

You can check running following cmds: Ubuntu nodejs version: nodejs -v
Version you installed: node -v

You gotta make sure you're using the one you installed.
Since it's probably a newer version which will have the semver package already.

Personally, I installed nvm and then ran: nvm install node (should install the latest version)
following with: nvm list or nvm ls to see your available versions.

If you're not using the latest version you've just installed then run:
nvm use <version>
In my case, as of the time writing this, my version is 19.8.0
So I'd use nvm use 19.8.0

then try to run npm & whatever command didn't work before.

You can also set the node default version with this command:
nvm alias default 19.8.0
if any problems in the future check again what version you're using with nvm ls.