react如何清理
清理 React 项目的方法
清理未使用的依赖项
运行 npm prune 或 yarn install --production 可以移除 node_modules 中未在 package.json 中声明的依赖。结合工具如 depcheck 能进一步识别未使用的依赖:
npx depcheck
删除构建产物
删除 build、dist 或类似的输出目录。对于 Create React App 项目,可通过以下命令清理:
rm -rf build/
清理缓存
清除 npm 或 Yarn 的缓存以释放空间:

npm cache clean --force
yarn cache clean
重置开发环境
删除临时文件如 .eslintcache 或 node_modules/.cache 目录。对于 Webpack 项目,可删除缓存文件夹:
rm -rf node_modules/.cache
优化代码结构
使用 ESLint 和 Prettier 自动格式化代码,移除未使用的变量或冗余代码。配置 ESLint 规则如 no-unused-vars 可帮助检测:

{
"rules": {
"no-unused-vars": "error"
}
}
清理全局安装的包
列出并移除全局安装的旧版本包:
npm list -g --depth=0
npm uninstall -g <package-name>
自动化清理脚本
在 package.json 中添加自定义脚本,例如:
{
"scripts": {
"clean": "rm -rf build node_modules && npm install"
}
}






