使用sudo进行npm安装时权限问题
# 0.前言
这个报错是使用 npm 执行全局 modules 安装时发生的,
> sudo npm install -g vsce
# 依旧提示
WARN install EACCES: permission denied, access '/Users/.../.npm'
1
2
3
2
3
查看 npm 环境:
> node -v
v14.17.6
> npm -v
6.14.15
1
2
3
4
2
3
4
# 1.错误原因
在npm v6版本中,npm对用户的权限有如下说明:
如果 npm 是由 root privileges 触发的(即,在mac中使用了sudo),则 npm 会默认以 nobody 的身份开启。如果需要仍需要使用root权限,则在script脚本中添加 unsafe-perm 的flag。
官网对此的原文:
# User
If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the
userconfig, which defaults tonobody. Set theunsafe-permflag to run scripts with root privileges.
# 2. 解决
升级
npm的版本,在v7版本和v8版本中使用sudo将默认使用root权限。在命令行中,添加
flag字段,即sudo npm install -g vesc --unsafe-perm1
# 参考
编辑 (opens new window)
上次更新: 2022/04/06, 15:04:00