The missing `yarn audit fix`
yarn-audit-fix
The missing yarn audit fix
yarn audit detects vulnerabilities but cannot fix them. The authors suggest Dependabot or Snyk, which is inconvenient in many setups. Discussion: yarn/issues/7075.yarn audit does not support custom (in-house) registries — see this issue & PR, still unmerged.yarn-audit-fix fetches advisories straight from the registry (the npm bulk
advisory endpoint) and patches the lockfile graph directly via lockgraph
(kudos to G. Kosev, code reference).
Full description: dev.to/yarn-audit-fix-for-yarn-2-berry
lockgraph.| Yarn | Lockfile schema | Supported |
|---|---|---|
| 1.x | yarn-classic |
✅ |
| 2.x | yarn-berry-v4 |
✅ |
| 3.0 | yarn-berry-v5 |
✅ |
| 3.1+ | yarn-berry-v6 |
✅ |
| 4.0–4.13 | yarn-berry-v8 |
✅ |
| 4.14+ | yarn-berry-v9 |
✅ |
| 5.x dev | yarn-berry-v10 |
✅ |
Node.js: >=18.12 — the highest floor in the shipped dependency closure (@yarnpkg/libzip / @yarnpkg/fslib); lockgraph itself needs only >=14.18. yaf declares no engines of its own.
yarn add yarn-audit-fix -Dor run it directly:
npm_config_yes=true npx yarn-audit-fix$ yarn-audit-fix [--opts]
Verifying package structure... Patching yarn.lock with audit data... Upgraded deps (1): [email protected] → 1.2.6 [critical, CVSS 9.8] GHSA-xvch-5gv4-984h Done
The lockfile is patched in place — the fix versions, their new transitive
dependencies, and (for yarn berry) the package checksums are all resolved
straight from the registry, so there's no reconcile yarn install step.
| Option | Description | Default |
|---|---|---|
--audit-level |
Include a vulnerability with a level as defined or higher. Supported values: low, moderate, high, critical | all |
--cwd |
Current working dir | process.cwd() |
--dry-run |
Get an idea of what audit fix will do | |
--json |
Print the outcome as JSON — { dryRun, upgraded, skipped, excluded, noFix } — instead of the human summary; pairs with --dry-run for a machine-readable preview. |
false |
--force |
Apply cross-major fixes: bump past a consumer's declared range, and rewrite a direct-dep range in package.json (root or a workspace) that the fix falls outside. See Direct-dependency pins |
false |
--help/-h |
Print help message | |
--npm-path |
Switch to project's local npm version instead of system default. Or provide a custom path. system / local / |
system |
--registry |
Custom registry url | |
--silent |
Disable log output | false |
--verbose |
Switch log level to verbose/debug | false |
--exclude |
Packages to skip updating — comma-separated glob[@range] rules (e.g. lodash,@scope/*@>=2 |
Only apply a fix whose completed dependency closure runs on the given engine — --engines.node='>=18', =floor (infer from the installed tree), or bare --engines.node (the Node running the CLI). Repeatable per engine. See Constraints. |
--license.allow / --license.deny |
Only apply a fix whose closure's SPDX licenses pass the policy — --license.allow=MIT,ISC (or bare --license=MIT,ISC) / --license.deny=GPL-3.0. See Constraints. |
|
--package-type |
Only apply a fix whose closure stays require-able — cjs rejects an ESM-only dependency a CommonJS tree can't require(). See Constraints. |
|
--on-conflict |
What to do when a fix can't satisfy the engine / license / package-type constraints: skip (leave it, report) or stop (error). |
skip |
--safe |
Fix only what's safe on every automatable axis (the opposite of --force): bundles --engines.node=floor + --package-type=cjs (the latter only in a CommonJS project). See Constraints. |
|
--production / --prod |
Fix only advisories reachable from production dependencies (dependencies / optionalDependencies / peerDependencies); dev-only ones are left and reported. See Scope. |
false |
--workspace |
Monorepo: fix only the selected workspaces' closures — comma-separated globs on a workspace's name / path / path-basename (e.g. @scope/core, packages/*, core,cli). See Scope. |
By default a fix is the lowest published version that clears the advisory. You can
add opt-in acceptance gates so a fix is only applied when its completed
dependency closure also satisfies a policy — otherwise the vulnerability is left
in place and reported (or, with --on-conflict=stop, the run errors).
--engines.node='>=18' accepts a fix only if every new package it
pulls in runs on Node ≥ 18. This catches the case where a security bump quietly
drags in a transitive that needs a newer runtime than your project targets — the
install passes, but CI/production on the old Node breaks. A bare --engines.node
uses the Node running the CLI (printed in the report, since that may differ from
your project's target); pass an explicit range to be sure, or --engines.node=floor
to infer it from what your tree already requires (the highest engines floor
across the root package.json + installed node_modules) — a fix is then rejected
only if it would raise that floor. Any engine works (--engines.npm='>=9'),
lenient by npm parity — a package that declares no engines is accepted.--license.allow=MIT,ISC,Apache-2.0 (or --license.deny=GPL-3.0)
accepts a fix only if the SPDX license of every new package it pulls in passes
the allow/deny policy. A bare --license=MIT,ISC is an allow list. An
unresolvable SPDX expression ((MIT OR X)) is flagged, not silently accepted.--package-type=cjs accepts a fix only if every new
package it pulls in stays require-able: a dependency that has gone ESM-only
("type": "module" with no CJS main/exports) would break every require()
of it in a CommonJS tree, so the fix is left for you to take deliberately. It's a
consistency check on entry points, not a runtime one — yaf can't prove your code
runs, but it can catch a fix that flips a dependency's module format.Every skip is attributed in the report — the axis, the blocking package, and the
versions tried — so you can widen the target, --exclude the package, or accept
the newer dependency:
Constraints: node >=18
Skipped (constraints — no fix keeps the closure within the policy [node >=18]; …):
[email protected] → 4.18.0 — needs some-dep@^2.0.0Constraints gate a fix's dependency closure (the transitives it pulls in), not the fixed package's own
engines/license.
--safe rolls the automatable gates into one flag — the opposite of --force.
It's shorthand for --engines.node=floor (don't raise the engine floor your tree
already stands on) plus --package-type=cjs in a CommonJS project (don't introduce
an ESM-only dependency). So yarn-audit-fix --safe means fix everything you can
without changing what my project runs on or how it loads — and flags the rest for
a deliberate follow-up. (License stays an explicit policy; --safe doesn't guess it.)
If a fix falls outside a direct dependency's declared range in package.json (an
exact pin like "lodash": "4.17.11", or a range that can't reach the fix), the
package is flagged and skipped by default — the fix is never silently applied
behind a manifest that forbids it. Re-run with --force to rewrite that range in
place (preserving the ^ / ~ / exact operator) and apply the bump, matching
npm audit fix --force. In a monorepo the pin is found and rewritten in the
workspace package.json that declares it — the report names the file:
Skipped (paNo open issues yet, or sync has not completed.