yarn upgrade to fix yarn audit errors
The solution to this problem in yarn is called selective version resolutions which is basically defining resolutions
for the transitive dependencies in the package.json
.
The transitive dependencies
are the dependencies of dependencies.
{
"resolutions": { "**/**/lodash": "^4.17.12" }
}
So here even if the lodash isn't a direct dependency of your package, the dependent package in your package uses the version defined in the resolutions. Specific resolutions can also be provided. More info here.
While resolutions
work, it is not the optimal solution, because:
- you clutter your
package.json
with resolutions of transitive dependencies - you override the actually required version with what you think will work version. Assume
A
depends onB@^4.0.0
and you update B and resolve it to^4.3.2
. Some time later A gets an update and requiresB@^5.0.0
, but you still resolve B to^4.3.2
, which is not compatible anymore.
Here is another way to update transitive dependencies:
- delete the version of the dependency you want to update from
yarn.lock
- run
yarn install
This way you force yarn to resolve the dependency again and in most cases yarn will install a newer version of what you deleted from yarn.lock
.
Example: let's assume that you want to update vulnerable minimist@0.0.8
, then you need to delete an entry like this from your yarn.lock
:
minimist@0.0.8:
version "0.0.8"
resolved "http://10.0.0.1/repository/npm-registry/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=
and then run yarn install
.
If this doesn't help:
Try updating dependencies that are higher in the dependency chain:
- Run
yarn why <dependency>
to find out which packages pull it - Go up the chain and try deleting the upper dependency in the chain from
yarn.lock
and then runningyarn install
Example:
Here is an example, where we update a transitive dependency minimist
:
$ yarn why minimist
.....
=> Found "mkdirp#minimist@0.0.8"
info This module exists because "eslint#mkdirp" depends on it.
=> Found "optimist#minimist@0.0.10"
info This module exists because "jest#jest-cli#@jest#core#@jest#reporters#istanbul-reports#handlebars#optimist" depends on it.
.....
- Delete
minimist
entries from yarn.lock and runyarn install
-> this doesn't help, presumably becausemkdirp
andoptimist
require exactlyminimist@0.0.8
andminimist@0.0.10
- Delete "direct parents" of
minimist
from yarn.lock:mkdirp
andoptimist
. - Run
yarn install
. Run
yarn why minimist
again:$ yarn why minimist ..... => Found "mkdirp#minimist@1.2.5" info This module exists because "eslint#mkdirp" depends on it. => Found "optimist#minimist@0.0.10" info This module exists because "jest#jest-cli#@jest#core#@jest#reporters#istanbul-reports#handlebars#optimist" depends on it. .....
Here we see that
minimist@0.0.8
was updated tominimist@1.2.5
, butminimist@0.0.10
still exists.Delete the next dependency in the dependency chain from
yarn.lock
:handlebars
- Run
yarn install
- Run
yarn why minimist
- nothing changed,minimist@0.0.10
is still there. - Delete the next dependency in the chain from
yarn.lock
:istanbul-reports
- Run
yarn install
- Run
yarn why minimist
:minimist@0.0.10
is not there anymore, becauseistanbul-reports
was updated.