├── .gitignore ├── bin └── nmcleaner.sh ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | -------------------------------------------------------------------------------- /bin/nmcleaner.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | 4 | basedir="$(pwd)" 5 | echo "[nmc] scanning $basedir" 6 | 7 | find "$basedir" -mindepth 1 -maxdepth 2 -type d -name node_modules | while IFS= read -r module; do 8 | echo "[nmc] cleaning $module" 9 | du -sh "$module" || true 10 | rm -rf "$module" 11 | done 12 | 13 | echo "[nmc] done" 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aowu-node-modules-cleaner", 3 | "version": "1.0.6", 4 | "description": "aowu-node-modules-cleaner", 5 | "main": "index.js", 6 | "bin": { 7 | "nmc": "bin/nmcleaner.sh" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "author": "aowu", 13 | "license": "ISC" 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aowu-node-modules-cleaner 2 | node_modules cleaner (node_modules 清理器) 3 | 4 | 5 | ## 安装 6 | 7 | 8 | ``` 9 | npm install -g aowu-node-modules-cleaner 10 | ``` 11 | 12 | 13 | ## 使用 14 | 15 | 在需要清理`node_modules`的目录下执行以下命令 16 | 17 | ``` 18 | nmc 19 | ``` 20 | 21 | ## node_modules 黑洞历史 22 | 23 | ![](https://fudongdong-statics.oss-cn-beijing.aliyuncs.com/images/20220213/b87c495bf2b44b46b690fbc8a1a9a207.png?x-oss-process=image/resize,w_800/quality,q_80) 24 | 25 | 1. npm安装模块一开始是树状结构,依赖的依赖都放到各自的node_modules目录下面,这样一个复杂一点项目安装完依赖,node_modules目录动不动就上G的大小。 26 | 2. 后来npm支持了扁平结构,但一旦遇到版本冲突,也就是依赖相同的模块但版本各不同,这样的依赖依旧会安装到各自的node_modules目录下,还是会有树状结构。 27 | 3. 所以node_modules就像黑洞一样,深不见底。有时候因为路径太长,系统都无法删除此类文件。 28 | 4. 以上也会导致,项目仅有几个页面,而`node_modules`居然有数个`G`的存储空间 29 | 30 | 31 | ## node_modules 清理器 32 | 33 | `cli`全局安装方法 34 | 35 | ```shell 36 | npm install -g aowu-node-modules-cleaner 37 | ``` 38 | 39 | 然后进入项目根目录下进行清理 40 | ```shell 41 | nmc 42 | ``` 43 | 44 | 该工具会递归所有目录(最大2层深度),将`node_modules`目录进行清理。 45 | 脚本使用`find`命令查找目标目录,能够正确处理包含空格的路径。 46 | --------------------------------------------------------------------------------