Uninstall everything npm related

First I tried removing all packages via this command:

npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm

But I get errors and the packages are still listed.

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', '-g', 'rm', 'semver' ]
2 info using npm@6.11.2
3 info using node@v12.9.1
4 verbose npm-session 1408ae41d1b54c1e
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 timing stage:loadCurrentTree Completed in 23ms
8 silly install loadIdealTree
9 silly install cloneCurrentTreeToIdealTree
10 timing stage:loadIdealTree:cloneCurrentTree Completed in 1ms
11 silly install loadShrinkwrap
12 timing stage:loadIdealTree:loadShrinkwrap Completed in 3ms
13 silly uninstall loadAllDepsIntoIdealTree
14 silly install loadAllDepsIntoIdealTree
15 silly fetchPackageMetaData error for semver@file:../../build/semver/src/semver-6.3.0.tgz ENOENT: no such file or directory, stat '/build/semver/src/semver-6.3.0.tgz'
16 timing stage:rollbackFailedOptional Completed in 0ms
17 timing stage:runTopLevelLifecycles Completed in 36ms
18 verbose stack Error: ENOENT: no such file or directory, stat '/build/semver/src/semver-6.3.0.tgz'
19 verbose cwd /home/dun
20 verbose Linux 4.19.69-1-MANJARO
21 verbose argv "/usr/bin/node" "/usr/bin/npm" "-g" "rm" "semver"
22 verbose node v12.9.1
23 verbose npm  v6.11.2
24 error code ENOENT
25 error syscall stat
26 error path /build/semver/src/semver-6.3.0.tgz
27 error errno -2
28 error enoent ENOENT: no such file or directory, stat '/build/semver/src/semver-6.3.0.tgz'
29 error enoent This is related to npm not being able to find a file.
30 verbose exit [ -2, true ]

Then I tried removing /usr/lib/node_modules and ~/.npm and /root/.npm but the global packages are still listed via npm list -g. How can I completely remove everything?

You get an error if you install a package from a tarball, delete the tarball, they try to uninstall the package. It looks like is what happened with semver.

You have deleted some likely places. What is your global prefix, where the packages are being found?

npm prefix -g

There is information about manually removing npm here: https://docs.npmjs.com/misc/removing-npm.html#more-severe-uninstalling

╰─ npm prefix -g
/usr

I removed the directory in /usr/lib but npm list -g still lists all the packages that were in there. I executed the commands from the documentation and used /usr/ as prefix but the packages are still listed.

I am baffled. What do these show:

 npm list -g --depth=0

and

ls -al /usr/lib/node_modules
╰─  npm list -g --depth=0
/usr/lib
├── @angular/cli@8.3.3
├── npm@6.11.2
└── semver@6.3.0

npm ERR! missing: node-gyp@^5.0.3, required by npm@6.11.2
npm ERR! missing: semver@^5.7.1, required by npm@6.11.2
npm ERR! missing: semver@2.x || 3.x || 4 || 5, required by init-package-json@1.10.3
npm ERR! missing: semver@^5.5.1, required by libnpmpublish@1.1.2
npm ERR! missing: semver@^5.4.1, required by lock-verify@2.1.0
npm ERR! missing: semver@2 || 3 || 4 || 5, required by normalize-package-data@2.5.0
npm ERR! missing: semver@^2.3.0 || 3.x || 4 || 5, required by npm-install-checks@3.0.0
npm ERR! missing: semver@~5.3.0, required by node-gyp@5.0.2
npm ERR! missing: semver@^5.6.0, required by npm-package-arg@6.1.1
npm ERR! missing: semver@^5.4.1, required by npm-pick-manifest@3.0.0
npm ERR! missing: semver@^5.6.0, required by pacote@9.5.8
npm ERR! missing: semver@2 || 3 || 4 || 5, required by read-installed@4.0.3
npm ERR! missing: semver@^5.1.0, required by package-json@4.0.1
npm ERR! missing: semver@^5.0.3, required by semver-diff@2.1.0


╰─ ls -al /usr/lib/node_modules
total 232
drwxr-xr-x   6 root root   4096 Sep 11 09:08 .
drwxr-xr-x 217 root root 208896 Sep 11 08:36 ..
drwxr-xr-x   3 root root   4096 Sep 11 09:07 @angular
drwxr-xr-x   9 root root   4096 Sep 11 08:36 node-gyp
drwxr-xr-x  10 root root   4096 Sep 11 08:36 npm
drwxr-xr-x   3 root root   4096 Sep 11 08:36 semver

Did you use sudo with the remove?

To try doing a clean uninstall of the packages then a manual delete of semver and anything left, you could try:

sudo npm uninstall -g @angular/cli
sudo npm uninstall -g node-gyp
sudo npm uninstall -g npm
sudo rm -rf /usr/lib/node_modules
sudo rm /usr/bin/semver
sudo npm uninstall -g node-gyp
sudo npm uninstall -g npm

Throw this error:

npm ERR! code ENOENT
npm ERR! syscall stat
npm ERR! path /build/node-gyp/src/node-gyp-5.0.2.tgz
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, stat '/build/node-gyp/src/node-gyp-5.0.2.tgz'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-09-11T09_37_37_368Z-debug.log

The last two commands work without an error but obviously I can’t use npm now anymore. So I reinstall npm via pacman and the global packages are still being listed. Maybe I shouldn’t install npm via pacman?

The listed dependencies of npm for archlinux do include node-gyp and semver, which are the two packages that you can not delete using npm. Makes some sense now. You can presumably uninstall those two with pacman.

https://www.archlinux.org/packages/community/any/npm/

(Do you want to delete npm itself too? I assumed the “uninstall everything npm related” included npm.)

I want to delete every global package because there are too many that I don’t use. I want to use npm afterwards. Sorry for not being specific.