乐闻世界logo
搜索文章和话题

How to remove a package from pnpm store, or force re-download it?

5 个月前提问
3 个月前修改
浏览次数207

2个答案

1
2

pnpm 是一种包管理工具,与 npmyarn 类似,但是它有自己独特的方式来管理包的存储。当你想要从本地存储中删除某个包,或者你想要强制重新下载某个包时,可以按照下面的步骤操作:

删除本地存储中的某个包

如果你需要从 pnpm 的全局存储中删除特定的包,可以使用 pnpm store prune 命令。这个命令会删除所有不被项目中的 package.json 文件依赖的包。

但是,如果你想要删除特定的包,可以手动去到 pnpm 的存储目录中删除对应的内容。pnpm 的存储目录通常在 ~/.pnpm-store

例如,要删除本地存储中的 lodash 包,你可以:

  1. 找到 lodash 包在本地存储中的位置。
  2. 直接删除该位置的相关文件和文件夹。

请注意,直接操作文件系统可能会导致 pnpm 的状态不一致,因此,请谨慎操作。

强制重新下载某个包

如果你想要强制重新下载某个包(也就是说,使 pnpm 忽略现有的缓存),你可以使用 pnpm install 命令配合 --force 参数。

例如,如果你想要重新下载 express 包,可以运行以下命令:

sh
pnpm install express --force

这个命令会告诉 pnpm 忽略本地存储中的缓存,而是去远程仓库下载最新的 express 包。

再举一个实际的场景,假设你在开发一个项目,发现一个依赖的包存在问题,你可能需要删除它从而确保下次运行 pnpm install 时可以下载新的副本。在这种情况下,除了使用 --force 参数,你还可以先用 pnpm remove 删除该依赖,然后再次添加它:

sh
# 删除依赖 pnpm remove lodash # 重新添加依赖,这会下载最新的版本 pnpm add lodash

这样做也会导致 pnpm 从远程仓库下载 lodash 包的最新版本。

结论

要从 pnpm 的本地存储中删除包或强制重新下载,你可以使用 pnpm store prune 清理未使用的包,直接删除存储中的文件和文件夹,或者通过安装命令结合 --force 参数来忽略缓存。在实际操作中,都需要小心谨慎,确保不会影响到其他依赖或项目的正常运作。

2024年6月29日 12:07 回复

{ My answer will cover pnpm v2.16.2 }

Short answer: run pnpm install --force. (pnpm update might work as well)

Long answer. When you just run pnpm install, pnpm compares the wanted shrinkwrap file (project/shrinkwrap.yaml) to the current one (project/node_modules/.shrinkwrap.yaml). They equal in your case, so node_modules is not touched.

When --force is used, packages are reverified and relinked from the store. Reverification means that its integrity is checked. You removed a file from jquery, so verification will fail and the package will be reunpacked to the store and relinked to node_modules.

Alternatively, you could remove your project's node_modules and run pnpm install. That would also check the integrity of jquery before linking it to the store.


That being said, I think pnpm install jquery should also probably verify the integrity of jquery. We'll create an issue for this in the pnpm repo.

And maybe we can add some additional commands for reverifying every package in node_modules and re-unpacking all modified dependencies.

A related command currently available is pnpm store status which prints a list of mutated packages

2024年6月29日 12:07 回复

你的答案