How can I fully uninstall Node.js on macOS and then reinstall it from scratch?

How can I fully uninstall Node.js on macOS and then reinstall it from scratch?

For macOS users who installed Node.js using Homebrew, here’s how you can completely uninstall Node.js and npm, and then reinstall them:

  1. Uninstall Node.js and npm:

    brew uninstall node
    brew cleanup
    rm -f /usr/local/bin/npm /usr/local/lib/dtrace/node.d
    rm -rf ~/.npm
    
  2. Install Node.js using Homebrew:

    brew install node
    

    After installation, verify the installation path with:

    which node  # Should output: /usr/local/bin/node
    
  3. Set NODE_PATH in ~/.bashrc:

    Edit ~/.bashrc and add the following line:

    export NODE_PATH='/usr/local/lib/node_modules'
    
  4. Consider using NVM:

    Instead of managing Node.js versions with Homebrew, you can use NVM (Node Version Manager) to manage multiple versions of Node.js. Here’s how you can use NVM:

It looks like you had multiple installations of Node.js and npm, which caused conflicts. To completely uninstall Node.js and npm from your macOS system and reinstall from scratch, follow these steps:

  1. Delete local Node.js and npm installations:

    sudo rm -rf /Users/myusername/local/{include,node_modules}
    
  2. Remove any remnants of Node.js and npm from /usr/local:

    sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
    

    Alternatively, you can use the following commands to achieve the same:

    sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* /usr/local/lib/dtrace/node.d ~/.npm ~/.node-gyp
    
  3. If you installed Node.js using Homebrew, uninstall it:

    brew uninstall node
    
  4. Check your Home directory for any remaining local, lib, or include folders related to Node.js and delete them.

  5. Check and remove any remaining node or node_modules directories from /opt/local:

    sudo rm -rf /opt/local/bin/node /opt/local/include/node /opt/local/lib/node_modules
    
  6. Also, remove any remaining npm executable and man pages:

    sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node.1 /usr/local/lib/dtrace/node.d
    
  7. Finally, if you had previously used NVM, make sure to revert any changes it made to your PATH variable in $HOME/.bashrc manually.

After completing these steps, you can download and install NVM (Node Version Manager) to manage your Node.js installations and then use it to install the latest version of Node.js and npm.

To completely uninstall Node.js and npm on macOS using Homebrew, follow these steps:

  1. Uninstall Node.js and npm:
brew uninstall node
  1. Remove any leftover Node.js files:
which node  # Verify that Node.js is uninstalled
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/local/lib/node_modules/npm/
  1. Check for any issues:
brew doctor
  1. Clean up Homebrew:
brew cleanup --prune-prefix

After these steps, Node.js and npm should be completely erased from your system.