Removing Node.js and npm from Linux Systems

Removing Node.js and npm from Linux Systems

Node.js is a popular JavaScript runtime environment, often used with the Node Package Manager (npm). Sometimes you may need to uninstall Node.js and npm from your Linux system, whether to upgrade versions, clean up space, or resolve conflicts. This tutorial provides a comprehensive overview of how to do so, covering various installation methods and ensuring a clean removal.

Understanding Installation Methods

The method for uninstalling Node.js depends heavily on how it was originally installed. Common methods include:

  • Package Managers (apt, yum, dnf): Using your distribution’s package manager (like apt on Debian/Ubuntu, yum or dnf on Fedora/CentOS/RHEL) is the most common and recommended approach.
  • Source Code: Compiling and installing from source code requires specific uninstall instructions.
  • Node Version Manager (nvm): nvm manages multiple Node.js versions and provides its own uninstall methods. (This tutorial won’t cover nvm specifically, as it’s a version management tool, not a direct installation method).
  • Pre-built Binaries: Downloading and extracting pre-built binaries.

Uninstalling with Package Managers

This is the simplest and most common scenario.

Debian/Ubuntu (apt):

Use the apt-get or apt command to remove Node.js:

sudo apt-get remove nodejs

This command also removes npm as it’s a dependency of the nodejs package. To purge configuration files as well, use:

sudo apt-get purge nodejs

Fedora/CentOS/RHEL (yum/dnf):

Use yum or dnf to remove Node.js:

sudo yum remove nodejs   # For older systems using yum
sudo dnf remove nodejs   # For newer systems using dnf

If you added a NodeSource repository during installation, you should remove it before completely uninstalling Node.js. First, list the repositories:

yum repolist all # or dnf repolist all

Then, remove the NodeSource repository (replace nodesource-nodejs with the exact repository name if it’s different):

sudo yum remove nodesource-nodejs # or sudo dnf remove nodesource-nodejs

Finally, clean any cached package data:

sudo yum clean all # or sudo dnf clean all

Uninstalling from Source Code

If you compiled and installed Node.js from source code, you’ll need to use the make command for uninstallation.

First, navigate to the directory where you originally extracted the Node.js source code. Then, run:

sudo make uninstall

Important: If you used a specific --prefix option during the configure step, you must use it again during the uninstall process. For example, if you configured with ./configure --prefix=$HOME/local/node, you’d need to run:

./configure --prefix=$HOME/local/node
sudo make uninstall

Manually Removing Node.js (Last Resort)

If the above methods fail, you can attempt a manual removal, but proceed with caution. Incorrectly removing files can cause system instability.

  1. Identify Node.js files: Use the which command to find the location of the Node.js executable:

    which node
    

    This will likely point to a directory like /usr/local/bin/node or /usr/bin/node.

  2. Remove the executable and related files: Carefully remove the Node.js executable and any associated files in the identified directory. The exact files to remove may vary, but could include:

    sudo rm -r /usr/local/bin/node
    sudo rm -r /usr/local/lib/node*
    sudo rm -r /usr/include/node*
    sudo rm /usr/local/share/man/man1/node.1
    

    Adapt these commands to the actual paths on your system.

Removing npm

npm is often uninstalled alongside Node.js when using package managers. However, if npm remains, or you installed it separately, you can try:

sudo npm rm npm -g

This attempts to remove npm globally. You may also find a .npm directory in your home directory (~/.npm) which can be manually deleted if it exists.

Verification

After uninstalling, verify that Node.js and npm are no longer accessible by running:

node -v
npm -v

These commands should return "command not found" or similar errors if the uninstallation was successful.

Leave a Reply

Your email address will not be published. Required fields are marked *