<root>/packages/project-one/package.json
{
"name": "project-one",
"dependencies": {
"react": "^17.0.0"
}
}
<root>/packages/project-two/package.json
{
"name": "project-two",
"dependencies": {
"react": "^18.0.0"
}
}
For reasons, we needed to set resolution rules for React to ensure that we were installing the correct version.
Here’s the rub: Yarn only allows you specify resolutions in the root workspace, and we needed separate resolution rules for each of the major versions of React that we are using.
To do that, you need to use the following syntax:
<root>/package.json
{
"resolutions": {
"react@^17.0.0": "17.0.2",
"react@^18.0.0": "18.2.0"
}
}
In general, if your dependency is listed in dependencies
or devDependencies
like this:
<root>/packages/<package-name>/package.json
{
"dependencies": {
"<name>": "<range>"
}
}
Then you can write a resolution rule like this:
<root>/package.json
{
"resolutions": {
"<name>@<range>": "<resolve-range>"
}
}
The yarn set resolution
command can be used to update the installed version of your dependency, and it will update your yarn.lock
file. However, be warned that the -s, --save
flag for this command is not implemented yet at the time of writing. So if you are like me and wondering why your root package.json
is not being updated by this command, that is why!