我可以查看我需要哪个 node_modules 吗?

Can I review which node_modules I need?

有没有办法查看我的 create-react-app 项目中使用了哪些 node_modules 包并删除未使用的模块?除了手动检查所有使用的包及其依赖项之外?

我想减少项目使用的 space 数量。它已提交给资源存储库,仅 node_modules 文件夹就使使用的 space 增加了三倍。

可能最简单的方法是通过将节点模块添加到 gitignore 文件(或创建一个)来不包含节点模块,但如果那不是一个选项,可能只是卸载不需要的包

对于回购大小(假设您使用的是 git):

  1. git rm -r --cached ./node_modules
  2. 将行 /node_modules 添加到您的 .gitignore 文件
  3. git add .
  4. git commit -m "Remove node_modules from repo"

构建大小:

create-react-app 非常擅长 tree shaking, so you shouldn't need to worry too much about this. Certain packages may have more modular ways of importing, which can help - usually these will be documented on a package-by-package basis. For example, Material UI icons 支持两种 import 语法:

// option 1
import AccessAlarmIcon from '@material-ui/icons/AccessAlarm'
import ThreeDRotation from '@material-ui/icons/ThreeDRotation'
// option 2
import { AccessAlarm, ThreeDRotation } from '@material-ui/icons'

其中,选项 1 可能会产生最小的捆绑包大小。

对于您开发机器上的大小:

你可以在这里做出的最大改变(假设你有其他项目)可能是切换到像 pnpm 这样的东西,它将 node_modules 集中存储在你的本地机器上,然后从你的项目,而不是有许多相同模块的实例。

一般客房清洁和整洁:

您可以尝试使用 depcheck 等工具来检测未使用的依赖项。不过,这可能有点矫枉过正,除非它是您项目的一个特别痛点。