npm cache clean --force 报错解决方法

栏目: NodeJs 发布时间:2024-12-05

执行 npm cache clean --force 命令遇到如下报错信息:

$ npm cache clean --force
npm WARN using --force I sure hope you know what you are doing.
npm ERR! code EACCES
npm ERR! syscall unlink
npm ERR! path /Users/teng/.npm/_cacache/index-v5/10/a5/91097af49aef49439e613e418c5be2707991a587cbf40e932a437e05d181
npm ERR! errno -13
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 501:20 "/Users/teng/.npm"

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/teng/.npm/_logs/2022-01-20T06_17_48_414Z-debug.log

“npm ERR! Your cache folder contains root-owned files” 这段字面描述很清楚,你的缓存目录中包含 root 权限的文件,所以因为你没有权限操作这个文件就报错了。

解决方法:

# 将缓存目录替换为你自己服务器/电脑的 npm 缓存目录
sudo chown -R 501:20 "/Users/teng/.npm"

我们再来执行 npm cache clean --force 就不会再报上述错误了。

本文地址:https://www.tides.cn/p_node-npm-error-your-cache-folder-contains-root-owned-files

标签: cache chown