惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
The Hacker News
The Hacker News
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
C
Cisco Blogs
AWS News Blog
AWS News Blog
T
Threatpost
L
LINUX DO - 热门话题
Simon Willison's Weblog
Simon Willison's Weblog
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
The Register - Security
The Register - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
罗磊的独立博客
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
N
News | PayPal Newsroom
Project Zero
Project Zero
NISL@THU
NISL@THU
博客园_首页
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Full Disclosure
T
Troy Hunt's Blog
Recorded Future
Recorded Future
N
Netflix TechBlog - Medium
P
Privacy International News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
Arctic Wolf
C
Check Point Blog
W
WeLiveSecurity
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes

Claude's Blog

浅谈 npm 依赖治理 记录 Got(Node.js) 代理 HTTP 请求的坑 如何管理桌面窗口 SSR 页面 CDN 缓存实践 从 is-promise 事件我们可以学到什么 分享一个 npm dist-tag 的冷知识 从 Vimium 到 qutebrowser Verdaccio 性能优化:单机 Cluster Verdaccio 性能优化:代理分流 Verdaccio 性能优化:上游路径转发 CSAPP DataLab 题解 不靠谱的 Egg.js 框架开发指南 如何解决 Debian 系 Elastic apm-server 7.x 启动失败 Hexo NexT 主题升级 7.4 From Journeyman to Master Mac 上移除 EasyConnect 常驻后台进程 Nginx SWRR 算法解读 记一次 Node.js 进程挂起的 BUG 追踪 警惕 Travis CI 的 npm 缓存
让 npm install 不使用缓存的方法
Post author: Claude Ray · 2019-12-06 · via Claude's Blog

Why

npm 的安装出错是屡见不鲜,往往是因为安装的环境不够 “clean”。

通常情况下,只要删除项目目录的 node_modules 和 package-lock.json,重新执行 install 就能解决。

偶尔也会出现上述操作解决不了的问题,譬如 npm 的缓存文件异常,就需要在安装前执行 npm cache clean --force 清空缓存目录。

但 npm cache clean 也存在两个未处理的缺陷,使它既不完全可靠又具备风险。

  1. 部分依赖会和 npm 共用缓存目录(终端下通过 npm config get cache 命令查看,默认 ~/.npm),用来存放自己的临时文件。

    而 npm@5 之后,cache clean 只会清除该缓存目录下的 _cacahce 子目录,而忽视不在该子目录的缓存。

    例如 @sentry/cli 将缓存放在了和 _cacache 同级的 sentry-cli 目录,clean cache 不会清除此处缓存。

    此例有网友专门记录了排错经过

  2. 突然执行 cache clean,将导致正在使用 npm install 的项目丢失部分依赖。

    如果有多个项目在同一环境执行 npm install,此问题的影响会进一步扩大,npm 将抛出各种文件操作错误。

鉴于缓存出错是极小概率事件,若能使用温和的安装方式避开缓存文件,无疑是更好的选择。

可是,npm install 利用缓存的行为是默认且强制的,目前官方还没有提供形如 –no-cache 的选项来做一次忽略缓存的干净安装。

npm-cache 机制详见官网文档

How

尽管 npm cli 还没支持,但这个需求我们自己实现起来却十分简单。

既然 cache 目录是通过 npm config get cache 获取的,也就支持相应的 set 方式。为每个待安装项目重新配置 cache 目录,等于变相地清除了 npm 之前所有的缓存。

当然,直接 npm config set cache 会让 npm 全局生效,为了单独设置缓存目录,在项目内添加 .npmrc 文件,并加入

1
cache=.npm

可观察到缓存路径的变更生效

1
2
3
4
5
6
7
$ npm config get cache
/Users/claude/.npm

$ cd ~/node-project && echo cache=.npm >> .npmrc

$ npm config get cache
/Users/claude/node-project/.npm

再安装就会重新下载依赖啦,还起到了环境隔离的作用。