依靠nvm 解决Mac 安装nrm 出现ERR的问题

问题描述

mac电脑

在终端执行npm i nrm -g时安装失败

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/nrm
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir /usr/local/lib/node_modules/nrm
npm ERR!  [Error: EACCES: permission denied, mkdir /usr/local/lib/node_modules/nrm] {
npm ERR!   errno: -13,
npm ERR!   code: EACCES,
npm ERR!   syscall: mkdir,
npm ERR!   path: /usr/local/lib/node_modules/nrm
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

原因分析:

The operation was rejected by your operating system. 您的操作系统拒绝此次操作。 It is likely you do not have the permissions to access this file as the current user 作为当前用户,您可能没有访问该文件的权限。 If you believe this might be a permissions issue, please double-check the permissions of the file and its containing directories, or try running the command again as root/Administrator. 如果你认为这是一个权限问题,请再次检查该文件和它所包含的目录权限,或者尝试以根/管理者再次运行该指令。

没有获得管理员权限,npm权限不足,要以管理员身份运行命令

临时解决办法:

在终端执行 sudo npm i nrm -g

然后输入电脑的管理员密码操作即可完成。

mac电脑输入密码时不会显示,输入正确的密码后直接回车即可。

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142

added 58 packages in 5s

11 packages are looking for funding
  run `npm fund` for details

本次安装成功,但我想在另一个项目安装nrm的时候还会出现同样的问题,治标不治本。

在网上搜索的时候,看到了这段话。

由于npm和node.js产品由不同的实体管理,更新和维护可能会变得复杂。而且,Node.js安装过程将npm安装在一个只有本地权限的目录中。当您试图全局运行包时,这可能会导致权限错误。 为了解决这两个问题,许多开发人员选择使用节点版本管理器(nvm)来安装npm。版本管理器将避免权限错误,并将解决更新Node.js和npm的复杂性。

以及当前我安装的是node的最新版本,旧的项目可能不支持,所以要切换到旧的node版本,因此需要通过nvm来管理node版本。


最终解决方案:

安装nvm,切换node版本。

这篇记录了nvm安装的过程和遇到的一些小问题。

经验分享 程序员 微信小程序 职场和发展