在 Linux 下修改 Node.js 的缓存配置,通常指以下几类,你可以按需选择:
npm config get cache
默认一般是:
~/.npm
npm config set cache /your/path/.npm
例如:
npm config set cache /data/node_cache/.npm
npm config set cache-max <size_in_MB>
或
npm config set maxsockets 5
npm cache clean --force
yarn config set cache-folder /your/path/yarn-cache
查看:
yarn cache dir
pnpm config set store-dir /your/path/pnpm-store
查看:
pnpm store path
Node.js 运行时的 require 缓存是内存级的,不能直接“改路径”,但可以:
delete require.cache[require.resolve('./module')]
node --no-cache
(Node 18+ 支持实验性)
npm config set prefix /your/path/node_global
并加到环境变量:
export PATH=/your/path/node_global/bin:$PATH
写入配置文件:
vim ~/.npmrc
示例:
cache=/data/node_cache/.npm
prefix=/data/node_global
registry=https://registry.npmmirror.com
如果你说的是 Docker / CI / 服务器空间不足 / 特定框架缓存,可以告诉我具体场景,我给你更精确的方案。