258

The Question is similar to How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X) but for Ubuntu, and just for uninstalling.

Installation was done by: sudo apt-get install node

How do I completely remove npm along with all libraries, packages and any other files installed by npm on my system, via e.g. npm install -g @vue/cli?

I do plan to reinstall npm afterwards.

E_net4
  • 27,810
  • 13
  • 101
  • 139
lpares12
  • 3,504
  • 4
  • 24
  • 45
  • 1
    installing nodejs-legacy works if you first `apt-get remove node` (or better yet, purge). The node program is a ham radio software modem so unless you have a ham license and want to send digital packets over ham radio you don't need it. – slebetman Sep 06 '15 at 19:03
  • 1
    yeah, I did it. Still I had problems. Everytime I wrote `node` in the console it would tell me something like `/usr/sbin/ couldn't finde node` or something like that. – lpares12 Sep 07 '15 at 02:30
  • Does this answer your question? [Uninstall Node.JS using Linux command line?](https://stackoverflow.com/questions/5650169/uninstall-node-js-using-linux-command-line) – Kulfy Aug 17 '20 at 15:03

7 Answers7

538
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 headache for me. You can install nodejs and npm without sudo using nvm.

Zanna
  • 205
  • 5
  • 13
Raja
  • 5,793
  • 1
  • 17
  • 20
  • 1
    Adding to this, I will also suggest you to remove any 3rd part PPAs if you have added them. I was stuck at this problem, node was getting removed but still I was unable to install a newer version without using `nvm` – Shivendra Soni Jul 12 '17 at 16:23
  • 12
    Only this worked for me! I just add the next command in the end: `rm -rf usr/local/bin/npm` – dani24 Dec 07 '17 at 03:03
  • If you want to remove `nvm` too check this issue https://github.com/creationix/nvm/issues/298 – Mouneer Apr 09 '18 at 09:44
  • answer above assumes that this was not installed using the node script and wget.... @zanna has name on both solutions, and the two should be combined – LeeRuns Dec 03 '20 at 22:05
  • @LeeRuns I only edited them. I agree there is a lot of duplication on this post - I think three nearly identical answers have already been removed. I don't think I can do anything further with the tools available to me. – Zanna Dec 04 '20 at 05:18
  • REMEMBER, it's `nodejs` and not `node`. I tried `apt remove node` but it couldnt find the package. but when I tried `apt remove nodejs` it worked – KMA Badshah Mar 13 '21 at 03:35
  • "you can see the location of the node. Try which nodejs and which npm too." What's your meaning on this? Since you still could see the node file, your solution to uninstall is still not complete, right? – Clock ZHONG Apr 17 '21 at 09:16
  • @dani24 adding onto your point, run this command with root privileges using `sudo` – Tanishq Vyas Dec 24 '21 at 05:56
  • Awesome. Can you please add some code at the end about how to install node using nvm - or a link - thx. – niico Aug 11 '22 at 13:27
188

It is better to remove NodeJS and its modules manually because installation leaves a lot of files, links and modules behind and later this creates problems when we reconfigure another version of NodeJS and its modules.

To remove the files, run the following commands:

sudo rm -rf /usr/local/bin/npm 
sudo rm -rf /usr/local/share/man/man1/node* 
sudo rm -rf /usr/local/lib/dtrace/node.d
rm -rf ~/.npm
rm -rf ~/.node-gyp
sudo rm -rf /opt/local/bin/node
sudo rm -rf /opt/local/include/node
sudo rm -rf /opt/local/lib/node_modules
sudo rm -rf /usr/local/lib/node*
sudo rm -rf /usr/local/include/node*
sudo rm -rf /usr/local/bin/node*

I have posted a step by step guide with commands on my blog: AMCOS IT Support For Windows and Linux: To completely uninstall node js from Ubuntu.

Zanna
  • 205
  • 5
  • 13
Amit Malik
  • 2,307
  • 1
  • 10
  • 5
132

Note: This will completely remove nodejs from your system; then you can make a fresh install from the below commands.

Removing Nodejs and Npm

sudo apt-get remove nodejs npm node
sudo apt-get purge nodejs

Now remove .node and .npm folders from your system

sudo rm -rf /usr/local/bin/npm 
sudo rm -rf /usr/local/share/man/man1/node* 
sudo rm -rf /usr/local/lib/dtrace/node.d 
sudo rm -rf ~/.npm 
sudo rm -rf ~/.node-gyp 
sudo rm -rf /opt/local/bin/node 
sudo rm -rf opt/local/include/node 
sudo rm -rf /opt/local/lib/node_modules  

sudo rm -rf /usr/local/lib/node*
sudo rm -rf /usr/local/include/node*
sudo rm -rf /usr/local/bin/node*

Go to home directory and remove any node or node_modules directory, if exists.

You can verify your uninstallation by these commands; they should not output anything.

which node
which nodejs
which npm

Installing NVM (Node Version Manager) by downloading and running a script

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

The command above will clone the NVM repository from Github to the ~/.nvm directory:

Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above says, you should either close and reopen the terminal or run the commands to add the path to nvm script to the current shell session. You can do whatever is easier for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm --version

which should give this output:

0.34.0

Installing Node.js and npm

nvm install node
nvm install --lts

Once the installation is completed, verify it by printing the Node.js version:

node --version

should give this output:

v12.8.1

Npm should also be installed with node, verify it using

npm -v

should give:

6.13.4

Extra - [Optional] You can also use two different versions of node using nvm easily

nvm install 8.10.0 # just put the node version number Now switch between node versions

$ nvm ls
->     v12.14.1
        v13.7.0
default -> lts/* (-> v12.14.1)
node -> stable (-> v13.7.0) (default)
stable -> 13.7 (-> v13.7.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.14.1)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.18.1 (-> N/A)

In my case v12.14.1 and v13.7.0 both are installed, to switch I have to just use

nvm use 12.14.1

Configuring npm for global installations In your home directory, create a directory for global installations:

mkdir ~/.npm-global

Configure npm to use the new directory path:

npm config set prefix '~/.npm-global'

In your preferred text editor, open or create a ~/.profile file if does not exist and add this line:

PATH="$HOME/.npm-global/bin:$PATH"

On the command line, update your system variables:

source ~/.profile

That's all

Zanna
  • 205
  • 5
  • 13
Nitin Raturi
  • 1,505
  • 1
  • 7
  • 12
  • 5
    This is the most comprehensive answer to removing old versions, and using NVM to make intalling the latest version easier. – traderjosh Nov 30 '21 at 19:25
  • If you face the error `Provided file to checksum does not exist. ` Please view [this answer](https://stackoverflow.com/questions/67633785/nvm-can-not-install-nodejs-on-ubuntu-18-04) – Tanishq Vyas Dec 24 '21 at 06:00
  • Perfect! This was butter smooth. – Neel Prajapati Oct 24 '22 at 03:45
  • You can either `Reinstall npm with a node version manager (recommended) or Manually change npm's default directory`. Doing both is unnecessary `https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally` – lance2k Aug 16 '23 at 06:51
7

It bothered me too much while updating node version from 8.1.0 to 10.14.0

Here is what worked for me:

  1. Open terminal (Ctrl+Alt+T).

  2. Type which node, which will give a path something like /usr/local/bin/node

  3. Run the command sudo rm /usr/local/bin/node to remove the binary (adjust the path according to what you found in step 2). Now node -v shows you have no node version

  4. Download a script and run it to set up the environment:

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

    Note: If you are getting error like

    node /usr/bin/env: node: No such file or directory
    

    just run

    ln -s /usr/bin/nodejs /usr/bin/node
    

    Source

  6. Now node -v will give v10.14.0

Worked for me.

jokerdino
  • 2,047
  • 1
  • 25
  • 31
S.Yadav
  • 4,273
  • 3
  • 37
  • 44
6

I was crazy to delete node and npm and nodejs from my Ubuntu 14.04 but with this steps you will remove it:

sudo apt-get uninstall nodejs npm node
sudo apt-get remove nodejs npm node

If you uninstall correctly and it is still there, check these links:

You can also try using find:

find / -name "node"

Although since that is likely to take a long time and return a lot of confusing false positives, you may want to search only PATH locations:

find $(echo $PATH | sed 's/:/ /g') -name "node"

It would probably be in /usr/bin/node or /usr/local/bin. After finding it, you can delete it using the correct path, eg:

sudo rm /usr/bin/node
jokerdino
  • 2,047
  • 1
  • 25
  • 31
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
6

Those who installed node.js via the package manager can just run:

sudo apt-get purge nodejs

Optionally if you have installed it by adding the official NodeSource repository as stated in Installing Node.js via package manager, do:

sudo rm /etc/apt/sources.list.d/nodesource.list

If you want to clean up npm cache as well:

rm -rf ~/.npm

It is bad practice to try to remove things manually, as it can mess up the package manager, and the operating system itself. This answer is completely safe to follow

Zanna
  • 205
  • 5
  • 13
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
-7

Try the following commands:

$ sudo apt-get install nodejs
$ sudo apt-get install aptitude
$ sudo aptitude install npm
Soroush Chehresa
  • 5,490
  • 1
  • 14
  • 29
Ruan Nawe
  • 363
  • 6
  • 9
  • 5
    The main difference between your answer and the accepted answer from a few years ago is that you're going through Aptitude. It'd be useful to explain why you feel this is a better approach than the accepted answer. What does Aptitude provide that the earlier answers were lacking? Why should readers implement your approach? – Jeremy Caney May 12 '20 at 23:52
  • 9
    Am I the only one who thought this is answering how to do the opposite of what the question is asking? – TheLebDev Mar 24 '21 at 13:17