2024-12-25 10:00:02

NPM清洁模块

有没有办法让npm取消构建node_modules下的所有模块?像npm rebuild这样的东西会删除所有构建构件,但不重新构建它们?


当前回答

我在package.json中添加了几行:

"scripts": {
  ...
  "clean": "rmdir /s /q node_modules",
  "reinstall": "npm run clean && npm install",
  "rebuild": "npm run clean && npm install && rmdir /s /q dist && npm run build --prod",
  ...
}

如果你只想清理,你可以使用这个rimraf node_modules。

其他回答

对于windows环境:

"scripts": {
    "clean": "rmdir /s /q node_modules",
    ...
}

我把这个添加到package.json中:

"build": "npm build",
"clean": "rm -rf node_modules", 
"reinstall": "npm run clean && npm install", 
"rebuild": "npm run clean && npm install && npm run build",

看起来效果不错。

NPM CI

正如FDisk所提到的,你可以使用npm ci删除节点模块文件夹,重新安装npm包。

npm马克西

或者,你也可以使用npm prune删除安装在node_modules文件夹中没有在package.json中定义的多余的包


引用:

如何清理不在package.json中的包的node_modules文件夹? NPM ci后修剪NPM

这项工作实际上有专门的命令

npm ci

它将删除node_modules目录,并根据您的package-lock安装包。json文件

更多信息:https://docs.npmjs.com/cli/ci.html

试一试 https://github.com/voidcosmos/npkill

npx npkill

它将找到所有的node_module并允许您删除它们。