💭

How to uninstall NodeJS on your Mac

2022/11/13に公開

0. Prerequisite

  • Install NodeJS(v18.9.1) with the macOS installer.
  • Install NodeJS(v19.0.1) with Homebrew.

PC Info

  • Information on the PC used is as follows.
OS: macOS Ventura(v13)
Device: MacBook Pro (13-inch, M1, 2020)
Chip: Apple M1
Memory: 16GB

1. Check current status

% brew list | grep node
node
%
% node -v
v19.0.1
% which node
/opt/homebrew/bin/node
% npm -v
8.19.2
% which npm
/opt/homebrew/bin/npm
%
% yarn -v
1.22.19
% which yarn
/opt/homebrew/bin/yarn
%
% ls -a .npm/
. ..  _logs
%

2. Delete File related to NodeJS

% brew uninstall node
Uninstalling /opt/homebrew/Cellar/node/19.0.1... (1,991 files, 50.9MB)
% node -v
v18.9.1
% which node
/usr/local/bin/node
% npm uninstall -g npm
% which npm
/usr/local/bin/npm
%
% lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | head
./usr/local/bin/corepack
./usr/local/bin/node
./usr/local/include/node/common.gypi
./usr/local/include/node/config.gypi
./usr/local/include/node/cppgc/common.h
./usr/local/include/node/js_native_api.h
./usr/local/include/node/js_native_api_types.h
./usr/local/include/node/libplatform/libplatform-export.h
./usr/local/include/node/libplatform/libplatform.h
./usr/local/include/node/libplatform/v8-tracing.h
% 
% lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | tail
./usr/local/lib/node_modules/corepack/shims/pnpx.ps1
./usr/local/lib/node_modules/corepack/shims/yarn
./usr/local/lib/node_modules/corepack/shims/yarn.cmd
./usr/local/lib/node_modules/corepack/shims/yarn.ps1
./usr/local/lib/node_modules/corepack/shims/yarnpkg
./usr/local/lib/node_modules/corepack/shims/yarnpkg.cmd
./usr/local/lib/node_modules/corepack/shims/yarnpkg.ps1
./usr/local/share/doc/node/gdbinit
./usr/local/share/doc/node/lldb_commands.py
./usr/local/share/man/man1/node.1
%
% lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read i; do echo "i= ${i:1}"; done 
% 
% lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read i; do sudo rm ${i:1}; done
% 
% sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /usr/local/include/node
% 
% ls /var/db/receipts/ | grep node
org.nodejs.node.pkg.bom
org.nodejs.node.pkg.plist
org.nodejs.npm.pkg.bom
org.nodejs.npm.pkg.plist
%
% sudo rm -f /var/db/receipts/org.nodejs.*
% ls /var/db/receipts/ | grep node
%

3. Check current status

% brew list | grep node
% node -v
zsh: command not found: node
% npm -v
env: node: No such file or directory
% yarn -v
env: node: No such file or directory
%

4. Install NodeJS with Homebrew

% brew install node
% 
% node -v
v19.0.1
% which node
/opt/homebrew/bin/node
%
% npm -v
8.19.2
% which npm
/opt/homebrew/bin/npm
%
% npm install --global yarn
% yarn -v
1.22.19
% which yarn
/opt/homebrew/bin/yarn
%

appendix: uninstall-node.sh for M1 Mac

#!/bin/sh
(( ${#} > 0 )) || {
  echo 'DISCLAIMER: USE THIS SCRIPT AT YOUR OWN RISK!'
  echo 'THE AUTHOR TAKES NO RESPONSIBILITY FOR THE RESULTS OF THIS SCRIPT.'
  echo "Disclaimer aside, this worked for the author, for what that's worth."
  echo 'Press Control-C to quit now.'
  read
  echo 'Re-running the script with sudo.'
  echo 'You may be prompted for a password.'
  sudo ${0} sudo
  exit $?
}
# This will need to be executed as an Admin (maybe just use sudo).

for bom in org.nodejs.node.pkg.bom org.nodejs.pkg.bom; do

  receipt=/var/db/receipts/${bom}
  [ -e ${receipt} ] && {
    # Loop through all the files in the bom.
    lsbom -f -l -s -pf ${receipt} \
    | while read i; do
      # Remove each file listed in the bom.
      # rm -v /usr/local/${i#/usr/local/}
      rm -v ${i:1}
    done
  }

done

# Remove directories related to node.js.
rm -vrf /usr/local/lib/node \
  /usr/local/lib/node_modules \
  /var/db/receipts/org.nodejs.*

exit 0

To run this, you can try:

% chmod +x ./uninstall-node.sh
% ./uninstall-node.sh
% rm uninstall-node.sh

FYI:
https://gist.github.com/nicerobot/2697848/

Discussion