还记得前一篇文章嘛?没错,我正式变成了一位 Mac 用户。关于购买它的心得,我将会出一期视频送给大家,到时候一定记得捧场喔!欢迎使用 RSS 订阅 我的日记,更快获得我的最新动作!

我的小窝(自创平台)已经日渐完善,但本博客暂时不会停止更新,依旧会整理出最详细的内容。我的日记呢,更多的是记录我的日常,以及一些遇到的小问题。

前几天给这台 MacBook Pro 安装 NodeJS 环境,貌似是使用了官网下载的 .pkg 安装包。安装过程非常简单,只需要点下一步那种就可以。但有个细节,就是它貌似需要我使用密码或者指纹进行了授权。

今天在 @Innei 大佬的指导下,打算给电脑安装一个名为“http-server”的 NPM 包,但是发现安装任何包都需要带上 sudo 开头(就是强制使用管理授权),我觉得这很不合理。毕竟此前在 Windows 下安装什么东西,都是直接 npm install 的。

他解释说,一般情况下用 Mac 是不需要使用 sudo 命令的,特别坑!这其实就是因为官网的版本需要在 root 下安装,所以导致后期安装任何包都需要授权!

所以,千万不要在官网上下载 NodeJS 了!

想要卸载使用 PKG 安装的 NodeJS 也非常麻烦,零散的文件必须手动删除。还好,我之前安装了 Brew,它有个实用工具可以检测安装软件可能存在的冲突,通过命令 brew doctor 即可进入。

[email protected] bin % brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Broken symlinks were found. Remove them with `brew cleanup`:
  /usr/local/bin/npm
  /usr/local/bin/npx
  /usr/local/lib/node_modules/npm/node_modules/.bin/mkdirp
  /usr/local/lib/node_modules/npm/node_modules/.bin/nopt
  /usr/local/share/man/man1/npm-README.1
  /usr/local/share/man/man1/npm-access.1
  /usr/local/share/man/man1/npm-adduser.1
  /usr/local/share/man/man1/npm-audit.1
  /usr/local/share/man/man1/npm-bin.1
  /usr/local/share/man/man1/npm-bugs.1
  /usr/local/share/man/man1/npm-build.1
  /usr/local/share/man/man1/npm-bundle.1
  /usr/local/share/man/man1/npm-cache.1
  /usr/local/share/man/man1/npm-ci.1
  /usr/local/share/man/man1/npm-completion.1
  /usr/local/share/man/man1/npm-config.1
  /usr/local/share/man/man1/npm-dedupe.1
  /usr/local/share/man/man1/npm-deprecate.1
  /usr/local/share/man/man1/npm-dist-tag.1
  /usr/local/share/man/man1/npm-docs.1
  /usr/local/share/man/man1/npm-doctor.1
  /usr/local/share/man/man1/npm-edit.1
  /usr/local/share/man/man1/npm-explore.1
  /usr/local/share/man/man1/npm-fund.1
  /usr/local/share/man/man1/npm-help-search.1
  /usr/local/share/man/man1/npm-help.1
  /usr/local/share/man/man1/npm-hook.1
  /usr/local/share/man/man1/npm-init.1
  /usr/local/share/man/man1/npm-install-ci-test.1
  /usr/local/share/man/man1/npm-install-test.1
  /usr/local/share/man/man1/npm-install.1
  /usr/local/share/man/man1/npm-link.1
  /usr/local/share/man/man1/npm-logout.1
  /usr/local/share/man/man1/npm-ls.1
  /usr/local/share/man/man1/npm-org.1
  /usr/local/share/man/man1/npm-outdated.1
  /usr/local/share/man/man1/npm-owner.1
  /usr/local/share/man/man1/npm-pack.1
  /usr/local/share/man/man1/npm-ping.1
  /usr/local/share/man/man1/npm-prefix.1
  /usr/local/share/man/man1/npm-profile.1
  /usr/local/share/man/man1/npm-prune.1
  /usr/local/share/man/man1/npm-publish.1
  /usr/local/share/man/man1/npm-rebuild.1
  /usr/local/share/man/man1/npm-repo.1
  /usr/local/share/man/man1/npm-restart.1
  /usr/local/share/man/man1/npm-root.1
  /usr/local/share/man/man1/npm-run-script.1
  /usr/local/share/man/man1/npm-search.1
  /usr/local/share/man/man1/npm-shrinkwrap.1
  /usr/local/share/man/man1/npm-star.1
  /usr/local/share/man/man1/npm-stars.1
  /usr/local/share/man/man1/npm-start.1
  /usr/local/share/man/man1/npm-stop.1
  /usr/local/share/man/man1/npm-team.1
  /usr/local/share/man/man1/npm-test.1
  /usr/local/share/man/man1/npm-token.1
  /usr/local/share/man/man1/npm-uninstall.1
  /usr/local/share/man/man1/npm-unpublish.1
  /usr/local/share/man/man1/npm-update.1
  /usr/local/share/man/man1/npm-version.1
  /usr/local/share/man/man1/npm-view.1
  /usr/local/share/man/man1/npm-whoami.1
  /usr/local/share/man/man1/npm.1
  /usr/local/share/man/man1/npx.1
  /usr/local/share/man/man5/folders.5
  /usr/local/share/man/man5/install.5
  /usr/local/share/man/man5/npmrc.5
  /usr/local/share/man/man5/package-json.5
  /usr/local/share/man/man5/package-lock-json.5
  /usr/local/share/man/man5/package-locks.5
  /usr/local/share/man/man5/shrinkwrap-json.5
  /usr/local/share/man/man7/config.7
  /usr/local/share/man/man7/developers.7
  /usr/local/share/man/man7/disputes.7
  /usr/local/share/man/man7/orgs.7
  /usr/local/share/man/man7/registry.7
  /usr/local/share/man/man7/removal.7
  /usr/local/share/man/man7/scope.7
  /usr/local/share/man/man7/scripts.7
  /usr/local/share/man/man7/semver.7

它告诉我有这么多东西冲突,可以看到这里面大多数都有 NPM 的影子。删除它们,可以使用 brew cleanup 命令。

[email protected] bin % brew cleanup
Error: Permission denied @ apply2files - /usr/local/lib/node_modules/npm/node_modules/.bin/mkdirp

结果,执行后直接提示没有权限。要知道,普通用户是无法删除 root 用户创建的文件的。之前安装 PKG 估计就是使用了 root 名义,才会变成了现在这个样子。

与此同时,我在上网继续搜索,得知有个名为 pkgutil 的工具,可以查询和卸载当前系统的 pkg 软件。

[email protected] bin % pkgutil --pkgs

看看是否有 nodenpm 相关的结果?我在 @Innei 的指导下用了一个名为 UninstallPKG 的软件进行卸载,在这已经没有了结果。不知道这个软件是否为此命令的 GUI 封装,还收费 9.99 刀,坑得一逼。


第二次执行了 brew doctor,发现少了两东西,分别是 /usr/local/bin/npm/usr/local/bin/npx。估计是那些文件夹不是 root 权限安装的,可以直接删除吧?

[email protected] bin % brew doctor               
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Broken symlinks were found. Remove them with `brew cleanup`:
  /usr/local/lib/node_modules/npm/node_modules/.bin/mkdirp
  /usr/local/lib/node_modules/npm/node_modules/.bin/nopt
  /usr/local/share/man/man1/npm-README.1
  /usr/local/share/man/man1/npm-access.1
  /usr/local/share/man/man1/npm-adduser.1
  /usr/local/share/man/man1/npm-audit.1
  /usr/local/share/man/man1/npm-bin.1
  /usr/local/share/man/man1/npm-bugs.1
  /usr/local/share/man/man1/npm-build.1
  /usr/local/share/man/man1/npm-bundle.1
  /usr/local/share/man/man1/npm-cache.1
  /usr/local/share/man/man1/npm-ci.1
  /usr/local/share/man/man1/npm-completion.1
  /usr/local/share/man/man1/npm-config.1
  /usr/local/share/man/man1/npm-dedupe.1
  /usr/local/share/man/man1/npm-deprecate.1
  /usr/local/share/man/man1/npm-dist-tag.1
  /usr/local/share/man/man1/npm-docs.1
...

那么剩下的这些,肯定就是 root 名义安装的东西了。在删除它们之前,我先用 brew 卸载掉前面安装错误的 NodeJS 吧!

[email protected] bin % brew uninstall node
Uninstalling /usr/local/Cellar/node/14.3.0... (4,659 files, 60.8MB)

@Innei 还建议我直接重装系统,我觉得太难了。还是打算先自己尝试一下,说不定就能解决问题了呢?

根据上方路径逐一进入,删除文件夹

[email protected] ~ % cd /usr/local/lib
[email protected] lib % sudo rm -rf node_modules

我也来验证一下我前面的判断,结果发现这些文件的所有者着实是 root

cd /usr/local/share/man/man1

ls -l

lrwxr-xr-x  1 root  wheel  49  5 20 23:21 npm-star.1 -> ../../../lib/node_modules/npm/man/man1/npm-star.1
lrwxr-xr-x  1 root  wheel  50  5 20 23:21 npm-stars.1 -> ../../../lib/node_modules/npm/man/man1/npm-stars.1
lrwxr-xr-x  1 root  wheel  50  5 20 23:21 npm-start.1 -> ../../../lib/node_modules/npm/man/man1/npm-start.1
lrwxr-xr-x  1 root  wheel  49  5 20 23:21 npm-stop.1 -> ../../../lib/node_modules/npm/man/man1/npm-stop.1
lrwxr-xr-x  1 root  wheel  49  5 20 23:21 npm-team.1 -> ../../../lib/node_modules/npm/man/man1/npm-team.1
lrwxr-xr-x  1 root  wheel  49  5 20 23:21 npm-test.1 -> ../../../lib/node_modules/npm/man/man1/npm-test.1
lrwxr-xr-x  1 root  wheel  50  5 20 23:21 npm-token.1 -> ../../../lib/node_modules/npm/man/man1/npm-token.1
lrwxr-xr-x  1 root  wheel  54  5 20 23:21 npm-uninstall.1 -> ../../../lib/node_modules/npm/man/man1/npm-uninstall.1
lrwxr-xr-x  1 root  wheel  54  5 20 23:21 npm-unpublish.1 -> ../../../lib/node_modules/npm/man/man1/npm-unpublish.1
lrwxr-xr-x  1 root  wheel  51  5 20 23:21 npm-update.1 -> ../../../lib/node_modules/npm/man/man1/npm-update.1
lrwxr-xr-x  1 root  wheel  52  5 20 23:21 npm-version.1 -> ../../../lib/node_modules/npm/man/man1/npm-version.1
lrwxr-xr-x  1 root  wheel  49  5 20 23:21 npm-view.1 -> ../../../lib/node_modules/npm/man/man1/npm-view.1
lrwxr-xr-x  1 root  wheel  51  5 20 23:21 npm-whoami.1 -> ../../../lib/node_modules/npm/man/man1/npm-whoami.1
lrwxr-xr-x  1 root  wheel  44  5 20 23:21 npm.1 -> ../../../lib/node_modules/npm/man/man1/npm.1
lrwxr-xr-x  1 root  wheel  44  5 20 23:21 npx.1 -> ../../../lib/node_modules/npm/man/man1/npx.1

为了保证删掉它们之后出现问题可以还原,这两个文件夹我选择移动到了桌面...

  • /usr/local/share/man/man5/
  • /usr/local/share/man/man7/

还有一个 /usr/local/bin/vue 一看就是 @vue/cli 的东西,直接删!

重新检测,brew 提示我的系统正常了!

[email protected] man % brew doctor         
Your system is ready to brew.
[email protected] man % brew install [email protected]

NodeJS 可以正常在 Brew 上安装了!爽歪歪,装 NPM 包再也不需要使用 sudo 了,成功解决!

使用 Brew 还有哪些好处呢?安装卸载只需要一条命令,由于文件存放比较合理,卸载也会更干净,不会出现前面 PKG 那样的严重残留问题。在这里也算是给大家安利一下啦。