【问题标题】:npm uninstall removes the package from package.json but doesn't remove it from node_modules foldernpm uninstall 将包从 package.json 中删除,但不会从 node_modules 文件夹中删除
【发布时间】:2021-10-11 17:36:22
【问题描述】:

我尝试使用删除包

npm uninstall (package_name) -s 

哪个从 package.json 中删除了包,但没有从 node_modules 文件夹中删除,那么如何从 node_modules 文件夹中删除这些未使用的包?

编辑:

我正在分享这个问答,因为我个人遇到了这个问题,使用了 npm uninstall 的所有变体但它没有从 node_modules 中删除包,所以我分享了帮助我删除大约 10 个npm prune

的未使用包

【问题讨论】:

  • 它应该从 node_modules 中删除它。
  • 它没有在我的机器上删除,我搜索了一段时间,直到我找到了这个解决方案,可以在不手动删除文件夹的情况下删除未使用的依赖项
  • 好的,对于 npm 的初学者,不清楚 npm 卸载是否不会从 node_modules 文件夹中删除,即使在 npm prune 的文档中它使用的描述并不简单,我花了大约半小时后才知道为什么 npm uninstall 没有删除它,甚至从这里的其他评论者看来他也这么认为(npm uninstall 将删除包文件夹)

标签: node.js angular npm angular5


【解决方案1】:

As per the npm guide,npm uninstall 仅从依赖项中删除包,它不会从 node_modules 中删除包文件夹(反正描述中没有提到)

出于某种原因,我认为 npm 卸载也会从 node_modules 中删除包文件夹,但它没有发生,经过一些研究我发现我们应该使用

npm prune

此命令将根据the official npm description自动从 node_modules 中删除未使用的包

npm 卸载只会从你的 package.json 文件,但它不会从你的 node_modules 中删除包

【讨论】:

  • 这很有用,我想知道为什么会有没有澄清的反对票:)
  • npm prune 不适合我,npm uninstall (package_name) -s 这是有效的
  • npm prune 不适用于全局包,因为没有 package.json 文件。找不到解决办法,npm uninstall -g 不会删除node_modules中的包文件夹。然后, npm list -g --depth 0 显示一些错误,因为找到了文件夹而不是 package.json 文件(文件夹仍然存在,但它是空的)。
  • 很高兴,很高兴我能提供帮助
【解决方案2】:

我喜欢分享我在从我的项目中卸载包的过程中遇到的困难。我相信它可能会为上述答案增加价值。

我在我的一个项目中评估了一个名为 react-router 的包。后来我决定删除它。 我想先删除包,然后从我的代码中删除它的引用。 我试过npm uninstallnpm prune。但是,在这两种情况下,它都无法从我的 node-modules 文件夹中删除包。 我从代码中删除了react-router 的引用并再次尝试。我仍然无法使用上述命令将其删除。 经过一番挖掘,我发现我还安装了使用包react-routerreact-router-dom。所以我必须先使用上面的命令删除它。只有这样,我才能删除react-router

我觉得奇怪的是npm uninstall 不会删除被引用的包。我认为,如果允许的话,我不必通过在类中搜索包而不是 IDE/Lint 来手动查找包的用法,这样可以帮助我更轻松地找到它。

【讨论】:

    【解决方案3】:

    如果您同时尝试了npm prunenpm uninstall --save,但模块仍然存在于node_modules,请尝试删除node_modules 文件夹:

    rm -rf node_modules
    

    然后运行

    npm install
    

    仅使用您需要的模块再次重建您的node_modules

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 2015-07-07
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 2017-01-06
      相关资源
      最近更新 更多