在 macOS 和 Linux 上清除 bundler 缓存

编辑页面

了解在 macOS 和 Linux 上使用 Yarn 或 npm 搭配 Expo CLI 或 React Native CLI 时,如何清除 bundler 缓存。


For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.

需要在 Windows 上清除开发缓存吗?在这里查找相关命令。

与你的项目相关联的缓存有很多种,它们可能会阻止项目按预期运行。清除缓存有时可以帮助你绕过与过期或损坏数据相关的问题,并且在排查和调试时通常很有用。

要清除各种缓存,请运行:

Expo CLI 和 Yarn

Terminal
# 使用 Yarn workspaces 时,你可能需要删除每个 workspace 中的 node_modules
rm -rf node_modules

yarn cache clean

yarn

watchman watch-del-all

rm -fr $TMPDIR/haste-map-*

rm -rf $TMPDIR/metro-cache

npx expo start --clear

Expo CLI 和 npm

Terminal
rm -rf node_modules

npm cache clean --force

npm install

watchman watch-del-all

rm -fr $TMPDIR/haste-map-*

rm -rf $TMPDIR/metro-cache

npx expo start --clear

React Native CLI 和 Yarn

Terminal
# 使用 Yarn workspaces 时,你可能需要删除每个 workspace 中的 node_modules
rm -rf node_modules

yarn cache clean

yarn

watchman watch-del-all

rm -fr $TMPDIR/haste-map-*

rm -rf $TMPDIR/metro-cache

yarn start -- --reset-cache

React Native CLI 和 npm

Terminal
rm -rf node_modules

npm cache clean --force

npm install

watchman watch-del-all

rm -fr $TMPDIR/haste-map-*

rm -rf $TMPDIR/metro-cache

npm start -- --reset-cache

这些命令在做什么

在网上找到命令后,先理解它们再运行是个好习惯。下面我们分别解释 Expo CLI、npm 和 Yarn 的每条命令,不过 React Native CLI 中对应的命令具有相同的行为。

命令描述
rm -rf node_modules清除项目的所有依赖项
yarn cache clean清除全局 Yarn 缓存
npm cache clean --force清除全局 npm 缓存
yarn/npm install重新安装所有依赖项
watchman watch-del-all重置 watchman 文件监视器
rm -rf $TMPDIR/<cache>清除指定的打包器/捆绑器缓存文件或目录
npx expo start --clear重新启动开发服务器并清除 JavaScript 转换缓存