How do you completely uninstall and reinstall Node.js and npm on macOS? | A1Lab@FahadM to JavaScriptEnglish • 6 days ago

How do you completely uninstall and reinstall Node.js and npm on macOS?

message-square
0

A clean uninstall and reinstall of Node.js and npm on macOS fixes broken PATH entries, version conflicts, and permission errors. Because macOS setups often mix Homebrew, the official .pkg installer, or version managers, removing leftover binaries and caches is essential before starting fresh.


1. Remove Existing Node.js and npm Installations

Run the steps that match how Node.js was installed on your Mac, then clear out global modules and caches:

  • If installed via Homebrew: Uninstall Node and prune old links/files:

    brew uninstall --force node brew cleanup

  • If installed via the official .pkg installer: Remove the installed binaries, libraries, man pages, and package receipts:

    sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*} sudo rm -rf /usr/local/lib/dtrace/node.d sudo pkgutil --forget org.nodejs.node.pkg sudo pkgutil --forget org.nodejs.npm.pkg

  • Clear npm caches and leftover state: Remove user-level caches and history directories:

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

  • Verify removal: Open a fresh terminal window and check that node and npm are no longer found (which node should return nothing or show a version manager path you intend to keep).

2. Reinstall Node.js and npm Cleanly

Pick a clean installation method depending on whether you want a single system install or multiple Node versions per project:

  • Option A — Homebrew (simple single-version setup): Install the node formula (which bundles npm) and verify the versions:

    brew install node node -v npm -v

  • Option B — Version manager (nvm or fnm): Ideal if you switch Node versions across projects without hitting global permission issues. After installing your chosen version manager and restarting your shell, install the latest LTS release:

    nvm install --lts nvm use --lts node -v npm -v

3. Common Gotchas After Reinstalling

  • Stale PATH entries in shell config: Check ~/.zshrc (or ~/.bash_profile) for old manual PATH exports pointing to deleted Node folders so your shell resolves the new binary cleanly.
  • Global packages are not carried over: Anything you previously installed with npm install -g will need to be reinstalled after a clean wipe.
  • Apple Silicon vs. Intel Homebrew paths: On Apple Silicon Macs, Homebrew lives under /opt/homebrew, whereas Intel Macs use /usr/local—avoid mixing old /usr/local Node leftovers with /opt/homebrew paths.

TL;DR: Remove Node via Homebrew or .pkg cleanup, delete ~/.npm and ~/.node-gyp caches, reinstall via Homebrew (brew install node) or a version manager (nvm install --lts), and verify with node -v and npm -v in a fresh terminal.

Did you originally install Node via Homebrew, the official .pkg installer, or a version manager like nvm?