Corepackとは
yarnやnpmなどのパッケージマネージャーを管理するツールです。
Corepackの使用方法
Corepackを有効化する
Corepackはデフォルトでは有効でないため、有効化する必要があります。
有効化するには以下のコマンドを実行します。
ターミナル
$ corepack enable
package.json
を編集する
package.json
に "packageManager"
を追記します。
package.json
{
"packageManager": "yarn@3.2.0"
}
上記のように記述した場合にはそのプロジェクトではyarnの3.2.0
が使用されるようになり、pnpmを使用すると以下のようなエラーが出ます。
ターミナル
$ yarn -v
3.2.0
$ pnpm
Usage Error: This project is configured to use yarn
しかし、npmの場合にはエラーが出ません。
ターミナル
$ npm
npm <command>
Usage:
npm install install all the dependencies in your project
npm install <foo> add the <foo> dependency to your project
npm test run this project's tests
npm run <foo> run the script named <foo>
npm <command> -h quick help on <command>
npm -l display usage info for all commands
npm help <term> search for help on <term>
npm help npm more involved overview
All commands:
access, adduser, audit, bin, bugs, cache, ci,
completion, config, dedupe, deprecate, diff,
dist-tag, docs, doctor, edit, exec, explain,
explore, find-dupes, fund, get, help, hook, init,
install, install-ci-test, install-test, link, ll,
login, logout, ls, org, outdated, owner, pack,
ping, pkg, prefix, profile, prune, publish,
rebuild, repo, restart, root, run-script, search,
set, set-script, shrinkwrap, star, stars, start,
stop, team, test, token, uninstall, unpublish,
unstar, update, version, view, whoami
Specify configs in the ini-formatted file:
/home/myuser/.npmrc
or on the command line via: npm <command> --key=value
More configuration info: npm help config
Configuration fields: npm help 7 config
npm@8.5.0 /usr/local/lib/node_modules/npm
npmをCorepackで管理する
npmにおいてもエラーを出すには以下のようにcorepack enable npm
する必要があります。
ターミナル
$ corepack enable npm
再度npmを使用するとエラーが出ます。
ターミナル¡¡
$ npm
Usage Error: This project is configured to use yarn
またバージョンの管理もできるようになります。
package.json
{
"packageManager": "npm@8.5.5"
}
ターミナル
$ npm -v
8.5.5