282

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.

3
  • 2
    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 6, 2015 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 7, 2015 at 2:30
  • Does this answer your question? Uninstall Node.JS using Linux command line?
    – Kulfy
    Aug 17, 2020 at 15:03

7 Answers 7

571
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.

9
  • 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 Jul 12, 2017 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 7, 2017 at 3:03
  • If you want to remove nvm too check this issue github.com/creationix/nvm/issues/298
    – Mouneer
    Apr 9, 2018 at 9: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 3, 2020 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 4, 2020 at 5:18
196

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.

4
  • 2
    Perfect, I've just added "sudo rm -rf ~/.npm-global" Dec 30, 2020 at 11:04
  • Under what circumstances would a system-installed Node write things in /opt, let alone then /usr/local?
    – tripleee
    Apr 11, 2021 at 6:03
  • I also needed 'sudo apt-get remove nodejs'
    – niico
    Aug 11, 2022 at 13:26
  • plus apt autoremove ?
    – gustav.b
    Nov 3, 2022 at 5:57
152

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

4
  • 7
    This is the most comprehensive answer to removing old versions, and using NVM to make intalling the latest version easier.
    – focus1691
    Nov 30, 2021 at 19:25
  • If you face the error Provided file to checksum does not exist. Please view this answer Dec 24, 2021 at 6:00
  • Perfect! This was butter smooth. Oct 24, 2022 at 3: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, 2023 at 6: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.

7

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

1
  • I don't like the manual way to delete things too. thank for this Jun 7, 2022 at 17:41
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
-7

Try the following commands:

$ sudo apt-get install nodejs
$ sudo apt-get install aptitude
$ sudo aptitude install npm
2
  • 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? May 12, 2020 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, 2021 at 13:17

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