“packageManager“: “pnpm@9.6.0“ 配置如何正确启动项目?
今天在学习开源项目的时候,在安装依赖时遇到了一个报错
yarn add pnpm@9.6.0 error This project's package.json defines "packageManager": "yarn@pnpm@9.6.0". However the current global version of Yarn is 1.22.22.Presence of the "packageManager" field indicates that the project is meant to be used with Corepack, a tool included by default with all official Node.js distributions starting from 16.9 and 14.19. Corepack must currently be enabled by running corepack enable in your terminal. For more information, check out https://yarnpkg.com/corepack
特此记录一下解决的过程。
原因
从项目的 package.json 文件中可以看到,项目已经指定了包管理器为 pnpm@9.6.0
...
"packageManager": "pnpm@9.6.0","engines": {"node": "22.4.1"}
这意味着该项目使用 Corepack 来管理其包管理器版本。但是我的当前终端环境中并没有启用 Corepack .
解决方案
-
启用
Corepack
在终端运行以下命令:
corepack enable
这个命令会设置 Corepack,使其自动根据 package.json 中指定的 packageManager 字段来使用正确的包管理器版本
-
检查 Corepack 是否启用并识别pnpm@9.6.0
corepack list
如果没有看到 pnpm@9.6.0,可以手动准备它:
corepack prepare pnpm@9.6.0 --activate
-
安装依赖
到此就可以按照正常的流程去安装依赖启动项目了。