6

The Question is similar to https://stackoverflow.com/questions/11177954/how-do-i-completely-uninstall-node-js-and-reinstall-from-beginning-mac-os-x but for Ubuntu, and mainly concerns the cleanup part.

Installation was done as usual by: sudo apt-get install node, however I later used the npm command to install multiple packages as more than one users.

How do I completely remove npm along with all libraries, packages and any other files installed by npm on my system by e.g. npm install -g @vue/cli? I would probably have to uninstall from apt, but the main question has to do with all the changes npm did.

Notes:

  1. I am on Ubuntu 20.04 LTS npm version 7.5.6 if that matters
  2. As there seems to be some fights about people uninstalling npm going on currently, please do not downvote just because I want to uninstall, I plan to reinstall :)
8

This will be messy ...

The simplest solution that I have found to this problem when trying to "fix" a broken development server that couldn't be formatted and rebuilt from scratch due to "management" is this:

  1. remove the globally installed packages
  2. delete the locally installed packages from the various /home directories and /root
  3. remove Node

Here's the basic process:

  1. Open Terminal (if it's not already open) or SSH into the machine (if you don't have physical access)
  2. List all globally installed packages:
    npm list -g --depth 0
    
  3. Uninstall the global packages one by one with:
    sudo npm uninstall -g <package-name>
    
    If you'd like to also do this for locally installed packages from your account, you can do this:
    npm list
    npm uninstall -S <package-name>
    
    The -S flag will also remove the reference in your package.json file
  4. Remove the following directories:
    /etc/npmrc
    /home/youruser/.npmrc
    /root/.npmrc
    ./.npmrc in any project directory next to package.json (search by using locate .npmrc)
  5. Remove the Node package:
    sudo apt remove nodejs --purge  
    sudo apt remove npm --purge  
    
  6. Finally, remove the straggling files and directories:
    sudo rm -rf /usr/local/bin/npm \ 
                /usr/local/share/man/man1/node* \
                /usr/local/lib/dtrace/node.d \
                ~/.npm \
                ~/.node-gyp \
                /opt/local/bin/node \
                /opt/local/include/node \
                /opt/local/lib/node_modules
    
    ... and the modules:
    sudo rm -rf /usr/local/lib/node*
    
    ... and the include modules:
    sudo rm -rf /usr/local/include/node*
    
    ... and the files in the local /bin:
    sudo rm -rf /usr/local/bin/node*
    
  7. Enjoy a 15-minute break, because you're done

At this point, you can reinstall Node and its package manager if you wish to start over with a clean slate.

2

The command I use to remove all packages from npm is..

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

Failing that (and sometimes npm can get very clingy)..

sudo rm -rf /usr/local/node_modules
rm -rf ~/.npm*
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.