Uninstalling Node.js, npm, and Node on Ubuntu: A Complete Guide

Introduction

Node.js is a popular runtime environment that enables developers to execute JavaScript code server-side. It’s often installed alongside npm (Node Package Manager), which facilitates the installation of various packages and libraries. However, there may be instances where you need to completely uninstall Node.js and npm from your Ubuntu system—for example, when troubleshooting issues or upgrading to a different version.

This tutorial will walk you through the steps required to remove Node.js, npm, and all associated files on an Ubuntu system. We’ll also explore how to reinstall these tools using a recommended approach for better management.

Step 1: Remove Node.js and npm via Package Manager

If Node.js was installed via apt, start by removing it along with its dependencies:

sudo apt-get remove --purge nodejs npm

The --purge option ensures that configuration files are also removed. After this step, update the package list to reflect these changes:

sudo apt-get update

Step 2: Clean Up Residual Files

Even after using apt-get, some residual files might remain. To ensure a thorough cleanup:

  1. Check for Node.js and npm paths:

    which node
    which nodejs
    which npm
    

    If these commands return any path, it indicates leftover binaries.

  2. Remove Residual Binaries:
    Manually delete any remaining files related to Node.js:

    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
    
  3. Remove Configuration and Cache Files:
    Clear out any configuration or cache files stored in your home directory:

    rm -rf ~/.npm
    rm -rf ~/.node-gyp
    
  4. Verify Removal:
    Run the which commands again to ensure they no longer return paths, confirming that Node.js and npm are fully removed.

Step 3: Reinstall Node.js using NVM (Node Version Manager)

NVM allows you to manage multiple versions of Node.js efficiently. Here’s how to set it up:

  1. Install NVM:
    Download and execute the installation script:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
    
  2. Load NVM in Your Shell:
    To start using nvm immediately, load it into your current shell session:

    export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    
  3. Verify NVM Installation:
    Check if nvm is installed correctly:

    nvm --version
    
  4. Install Node.js and npm using NVM:
    You can now install the latest stable version or a specific LTS version of Node.js:

    nvm install node  # Latest stable version
    nvm install --lts # Latest Long Term Support (LTS) version
    
  5. Switch Between Versions:
    If you have multiple versions installed, switch between them using:

    nvm use <version_number>
    

Step 4: Configure Global npm Packages

To manage global packages more effectively with nvm, set up a directory for global installations:

  1. Create the Directory:

    mkdir ~/.npm-global
    
  2. Configure npm to Use This Directory:
    Update npm’s configuration to use this new path:

    npm config set prefix '~/.npm-global'
    
  3. Update Your Environment Variables:
    Modify your shell profile to include the global bin directory in your PATH:

    echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.profile
    source ~/.profile
    

Conclusion

By following these steps, you’ve successfully removed Node.js and npm from your Ubuntu system and reinstalled them using NVM. This approach provides a flexible environment to manage multiple Node.js versions seamlessly, enhancing development workflows.

Leave a Reply

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