执行npm install -g firebase-tools遇EACCES权限错误求助
npm ERR! code EACCES npm ERR! syscall rename npm ERR! path /usr/local/lib/node_modules/firebase-tools npm ERR! dest /usr/local/lib/node_modules/.firebase-tools-ENG41nb9 npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/firebase-tools' -> '/usr/local/lib/node_modules/.firebase-tools-ENG41nb9' npm ERR! [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/firebase-tools' -> '/usr/local/lib/node_modules/.firebase-tools-ENG41nb9'] { npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'rename', npm ERR! path: '/usr/local/lib/node_modules/firebase-tools', npm ERR! dest: '/usr/local/lib/node_modules/.firebase-tools-ENG41nb9' 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. npm ERR! A complete log of this run can be found in: npm ERR! /home/avi/.npm/_logs/2023-02-19T08_22_34_901Z-debug-0.log
问题原因
当前用户没有对全局npm包目录/usr/local/lib/node_modules的读写权限,导致npm无法修改该目录下的文件。
解决方案
方案1:配置npm使用用户专属全局目录(推荐,无权限隐患)
- 创建用户专属的全局npm目录
mkdir -p ~/.npm-global
- 让npm使用这个新目录
npm config set prefix '~/.npm-global'
- 更新shell配置文件(根据你用的shell选择)
- bash用户:编辑
~/.bashrc,添加一行:
export PATH=~/.npm-global/bin:$PATH
- zsh用户:编辑
~/.zshrc,添加同样内容
- 让配置立即生效
# bash用户执行 source ~/.bashrc # zsh用户执行 source ~/.zshrc
- 现在无需sudo,直接执行安装命令
npm install -g firebase-tools npm install npm@latest -g
方案2:临时用sudo执行(不推荐,易引发后续权限混乱)
直接在命令前加sudo获取临时root权限:
sudo npm install -g firebase-tools sudo npm install npm@latest -g
注意:此方式安装的包后续操作可能仍需sudo,容易导致文件权限归属混乱,尽量避免。
方案3:修改现有全局目录的所有权
将/usr/local/lib/node_modules的所有权改为当前用户:
sudo chown -R $USER:$GROUP /usr/local/lib/node_modules
修改完成后即可正常执行全局安装命令。
内容的提问来源于stack exchange,提问作者Avinash Verma
相关产品推荐
相关产品推荐

