如何在npm上正确安装Leaflet?安装遇权限及文件已存在错误
解决npm安装Leaflet时的EACCES/EEXIST权限错误
错误信息
nasersobhan@Nasers-MacBook-Pro Library % npm i leaflet npm ERR! code EEXIST npm ERR! syscall mkdir npm ERR! path /Users/nasersobhan/.npm/_cacache/content-v2/sha512/20/8f npm ERR! errno EEXIST npm ERR! Invalid response body while trying to fetch https://registry.npmjs.org/leaflet: EACCES: permission denied, mkdir '/Users/nasersobhan/.npm/_cacache/content-v2/sha512/20/8f' npm ERR! File exists: /Users/nasersobhan/.npm/_cacache/content-v2/sha512/20/8f npm ERR! Remove the existing file and try again, or run npm npm ERR! with --force to overwrite files recklessly. npm ERR! Log files were not written due to an error writing to the directory: /Users/nasersobhan/.npm/_logs npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
解决方案
1. 手动删除冲突缓存目录
直接删除报错中提到的冲突缓存文件夹:
rm -rf /Users/nasersobhan/.npm/_cacache/content-v2/sha512/20/8f
之后重新执行安装命令:
npm i leaflet
2. 修复.npm目录权限
如果手动删除后仍报错,说明整个.npm目录权限存在问题,执行以下命令重置目录所有权:
sudo chown -R $USER:$USER /Users/nasersobhan/.npm
接着清理npm缓存:
npm cache clean --force
最后再次安装Leaflet:
npm i leaflet
3. 强制覆盖安装(谨慎使用)
按照错误提示,用--force参数强制覆盖冲突文件:
npm i leaflet --force
注意:该操作可能覆盖缓存中的其他文件,仅在前面方法无效时使用。
4. 用nvm避免全局权限问题(长期方案)
若频繁遇到这类权限问题,建议用nvm管理Node.js版本,它会将Node和npm安装在用户目录下,无需管理员权限:
- 卸载当前全局安装的Node.js和npm
- 安装nvm(本地用户级安装,无需sudo)
- 通过nvm安装Node.js:
nvm install node - 之后执行
npm i leaflet即可
内容的提问来源于stack exchange,提问作者Jamil Rahman Mowahid
相关产品推荐
相关产品推荐

