100

Steps to replicate:

Ubuntu 16.04 xenial (x86-64)

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

I want to uninstall nodejs and npm and have seen several posts which suggest this is the most common way:

sudo apt-get purge --auto-remove nodejs

However, this produces a prompt which seems to reference dependencies which, if they were removed, sound like they would cause problems.

What is the best way to "undo" the install of nodejs and npm?

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  gyp* libboost-python1.58.0* libjs-inherits* libjs-node-uuid*
  libjs-underscore* libssl-dev* libssl-doc* libuv1* libuv1-dev*
  linux-headers-4.4.0-18* linux-headers-4.4.0-18-generic*
  linux-headers-4.4.0-21* linux-headers-4.4.0-21-generic*
  linux-image-4.4.0-18-generic* linux-image-4.4.0-21-generic*
  linux-image-extra-4.4.0-18-generic* linux-image-extra-4.4.0-21-generic*
  linux-signed-image-4.4.0-18-generic* linux-signed-image-4.4.0-21-generic*
  node-abbrev* node-ansi* node-ansi-color-table* node-archy* node-async*
  node-block-stream* node-combined-stream* node-cookie-jar*
  node-delayed-stream* node-forever-agent* node-form-data* node-fstream*
  node-fstream-ignore* node-github-url-from-git* node-glob* node-graceful-fs*
  node-gyp* node-inherits* node-ini* node-json-stringify-safe* node-lockfile*
  node-lru-cache* node-mime* node-minimatch* node-mkdirp* node-mute-stream*
  node-node-uuid* node-nopt* node-normalize-package-data* node-npmlog*
  node-once* node-osenv* node-qs* node-read* node-read-package-json*
  node-request* node-retry* node-rimraf* node-semver* node-sha* node-sigmund*
  node-slide* node-tar* node-tunnel-agent* node-underscore* node-which*
  nodejs* nodejs-dev* npm* python-configobj* python-pycurl* python-pyexiv2*
  python-pyexiv2-doc*
0 to upgrade, 0 to newly install, 72 to remove and 0 not to upgrade.
After this operation, 631 MB disk space will be freed.
Do you want to continue? [Y/n]

Edit:

This is the output from sudo apt-get purge nodejs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  gyp libboost-python1.58.0 libjs-inherits libjs-node-uuid libjs-underscore
  libssl-dev libssl-doc libuv1 libuv1-dev linux-headers-4.4.0-18
  linux-headers-4.4.0-18-generic linux-headers-4.4.0-21
  linux-headers-4.4.0-21-generic linux-image-4.4.0-18-generic
  linux-image-4.4.0-21-generic linux-image-extra-4.4.0-18-generic
  linux-image-extra-4.4.0-21-generic linux-signed-image-4.4.0-18-generic
  linux-signed-image-4.4.0-21-generic python-configobj python-pycurl
  python-pyexiv2 python-pyexiv2-doc
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  node-abbrev* node-ansi* node-ansi-color-table* node-archy* node-async*
  node-block-stream* node-combined-stream* node-cookie-jar*
  node-delayed-stream* node-forever-agent* node-form-data* node-fstream*
  node-fstream-ignore* node-github-url-from-git* node-glob* node-graceful-fs*
  node-gyp* node-inherits* node-ini* node-json-stringify-safe* node-lockfile*
  node-lru-cache* node-mime* node-minimatch* node-mkdirp* node-mute-stream*
  node-node-uuid* node-nopt* node-normalize-package-data* node-npmlog*
  node-once* node-osenv* node-qs* node-read* node-read-package-json*
  node-request* node-retry* node-rimraf* node-semver* node-sha* node-sigmund*
  node-slide* node-tar* node-tunnel-agent* node-underscore* node-which*
  nodejs* nodejs-dev* npm*
0 to upgrade, 0 to newly install, 49 to remove and 0 not to upgrade.
After this operation, 27.6 MB disk space will be freed.
Do you want to continue? [Y/n]
3
  • 1
    Possibly many extraneous packages from autoremove. What happens when you just run sudo apt-get purge nodejs?
    – heemayl
    Commented Jun 12, 2016 at 9:43
  • How about trying stackoverflow.com/a/33947181/486919?
    – edwinksl
    Commented Jun 12, 2016 at 9:43
  • @heemayl - have added output to post, and just went ahead with that approach. Commented Jun 12, 2016 at 9:56

5 Answers 5

137

As seen from the output of:

sudo apt-get purge nodejs

it is only removing node related packages i.e. relevant packages, nothing more.

On the other hand, when you do:

sudo apt-get purge --auto-remove nodejs

it is essentially doing:

sudo apt-get purge nodejs
sudo apt-get autoremove

and the removal of the gyp, linux-headers-4.4.0-18-generic etc packages are actually triggered by autoremove as they were installed as dependencies and no longer needed by any installed package, presumably because the main package has been removed.

So it is perfectly fine in this context to run:

sudo apt-get purge --auto-remove nodejs

If you are too paranoid, you can do it in two steps: first purge nodejs:

sudo apt-get purge nodejs

and then remove the orphan dependencies (till now, if any):

sudo apt-get autoremove
26

To remove node js, npm and node_modules from Ubuntu, you need to remove containers also which are at different locations in Ubuntu. These could be as:

/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

I have posted the procedure to remove NodeJS on my blog: AMCOS IT Support For Windows and Linux: To completely uninstall node js from Ubuntu.

  1. First of all you need to run the following command from command terminal as sudo.

    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
    
  2. Remove node or node_modules directories from /usr/local/lib with the help of following command.

    sudo rm -rf /usr/local/lib/node*
    
  3. Remove node or node_modules directories from /usr/local/include with the help of following command.

    sudo rm -rf /usr/local/include/node*
    
  4. Remove any node file or dir from /usr/local/bin with the help of following command.

    sudo rm -rf /usr/local/bin/node*
    
  5. Go to home directory and remove any node or node_modules directory, if exists.

1
  • 4
    This was helpful - even after the purge commands, even after restarting, these binaries where still left over and acting like node/npm was still installed and all. Only after removing the binaries is a full uninstall complete.
    – Nikhil VJ
    Commented Sep 16, 2019 at 9:55
12

sudo apt-get remove nodejs

sudo apt-get remove npm

Then go to /etc/apt/sources.list.d and remove any node list if you have. Then do a

sudo apt-get update

Check for any .npm or .node folder in your home folder and delete those.

If you type

which node

you can see the location of the node. Try which nodejs and which npm too.

I would recommend installing node using Node Version Manager(NVM). That saved a lot of head ache for me. Because you can install nodejs and npm without sudo using nvm.

2
  • which command should I use to remove nodes in /etc/apt/sources.list.d ? what should I remove? I have nodesource.list! so probably sudo rm -rf /etc/apt/sources.list.d/nodesources.list* ?
    – doplano
    Commented Sep 10, 2020 at 5:48
  • Yes, run sudo rm -rf /etc/apt/sources.list.d/nodesources.list* to remove nodejs source from /etc/apt/sources.list.d directory. Commented Nov 17, 2020 at 5:42
6

To manually remove nodejs, npm and node_modules from Ubuntu, you need to do the following steps:

  1. Run the following command from the terminal as sudo:

     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      
    
  2. Remove node or node_modules directories from /usr/local/lib with the help of the following command:

     sudo rm -rf /usr/local/lib/node*                                                                                 
    
  3. Remove node or node_modules directories from /usr/local/include with the help of the following command:

     sudo rm -rf /usr/local/include/node*                                                                         
    
  4. Remove any node file or directory from /usr/local/bin with the help of the following command:

     sudo rm -rf /usr/local/bin/node*                                                                                
    
  5. Go to your home directory and remove any node or node_modules directory, if exists.

Now it is done.

1
  • 1
    This worked. The accepted answer (sudo apt-get purge --auto-remove nodejs) still left /usr/local/bin/node on my ubuntu 18.04 machine.
    – Garrett
    Commented May 9, 2020 at 2:33
5

I had this challenge when working on an Ubuntu 20.04 machine with Nodejs.

For me, I had to run the following commands to remove nodejs and all of it's dependencies.

This is to remove nodejs and it's dependencies:

sudo apt-get purge nodejs

This is to remove any dependencies that are still left after removing nodejs

sudo apt-get autoremove

This is to remove the nodejs repository from the /etc/apt/sources.list.d directory:

sudo rm -rf /etc/apt/sources.list.d/nodesource.list*

Update your package repository list:

sudo apt-get update

Confirm if the node executable is still available in your machine:

which node

That's all.

I hope this helps

You must log in to answer this question.

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