How To Completely Uninstall Node.js And NPM In Ubuntu?

Published August 22, 2024

Problem: Removing Node.js and NPM from Ubuntu

Uninstalling Node.js and NPM from an Ubuntu system can be difficult. These tools often leave files and settings behind, which can cause issues with new installations or other software.

Step-by-Step Guide to Uninstall Node.js and NPM

Removing Node.js Using Package Manager

To remove Node.js using the package manager, open a terminal and run this command:

sudo apt-get remove nodejs

This command removes the Node.js package from your system. To check the removal, use the which command:

which node

If Node.js is removed, this command should not return any output.

Tip: Verify Node.js Removal

After running the removal command, you can also check the Node.js version to confirm it's uninstalled:

node --version

If Node.js is successfully removed, this command should return an error message indicating that 'node' is not recognized.

Uninstalling NPM

To remove NPM and its packages, use this command:

sudo apt-get remove npm

After removing NPM, delete its config files by running:

rm -rf ~/.npm

Cleaning Up Remaining Files

To find and remove any leftover directories, check these locations:

  1. /usr/local/lib/node_modules
  2. /usr/local/include/node
  3. /usr/local/bin/node

You can remove these directories using the rm command:

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

To delete any remaining Node.js and NPM-related files, use this command:

sudo find / -name "*node*" -delete

This command searches for and removes any files or directories with "node" in their name. Be careful when using this command, as it may remove files you want to keep.

Verifying Complete Uninstallation

Checking for Residual Components

After following the uninstallation steps, check if Node.js and NPM are removed from your Ubuntu system. Here's how to check for remaining components:

  1. Use the which command to check for remaining executables:
which node
which npm

If these commands don't return output, the executables are no longer in your system's PATH.

  1. Check for remaining packages:
dpkg -l | grep node
dpkg -l | grep npm

These commands list installed packages with "node" or "npm" in their names. No results mean the packages are removed.

  1. Look for leftover directories:
ls -l /usr/local/lib/node*
ls -l /usr/local/include/node*
ls -l /usr/local/bin/node*

If these commands return "No such file or directory" messages, the directories are removed.

  1. Check for global npm packages:
npm list -g --depth=0

This command should return an error if NPM is uninstalled.

  1. Verify Node.js version:
node -v

This should return a "command not found" error if Node.js is removed.

By performing these checks, you can make sure Node.js and NPM are gone from your Ubuntu system. If you find remaining components, you may need to remove them manually using the right commands.

Tip: Check for Node.js Config Files

Look for any leftover configuration files in your home directory:

ls -la ~ | grep "\.npm"
ls -la ~ | grep "\.node"

If these files exist, you can remove them with:

rm -rf ~/.npm
rm -rf ~/.node