> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codemod.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dependencies

> Check which packages use a dependency, and bump, add, remove or rename it where it is declared, from a workflow.

> [!WARNING]
>
> **Experimental.** This can change in a minor release of the CLI, but not in a patch. Pin the CLI version (`npx codemod@<version>`) if you build on it.

Most migrations start and end with dependencies: run only if the repository uses a library, then move it to the new version (or swap it for another) in every `package.json` that declares it. `dependencies()` from `codemod/deps` does each of these in a line, in monorepos too, with pnpm, Yarn and bun catalogs. The full API is in the [reference](https://docs.codemod.com/community/workflows/reference.md#dependencies).

## Skip repositories that don't use the library

`dependencies()` runs inside a step. It reads the `package.json` files (and `pnpm-workspace.yaml` or `.yarnrc.yml` for workspaces and catalogs), so it works without a lockfile, and a lockfile that is missing or can't be read doesn't get in the way:

```ts workflow.ts theme={null}
import { step } from "codemod/workflow";
import { dependencies, semver } from "codemod/deps";

export default async function main() {
  const solid = await step("Solid versions", () =>
    dependencies()
      .declarations("solid-js", { kinds: ["prod", "dev"] })
      .map((d) => ({ dir: d.package.dir, range: d.resolved, major: semver.major(d.resolved ?? "") })),
  );
  if (!solid.length) return { skipped: "no package uses solid-js" };
  // ...
}
```

The repository's packages are the root `package.json` and the workspaces it declares (`workspaces`, or `packages` in `pnpm-workspace.yaml`, with `!` excludes). Without a workspace declaration that's the root alone, so a fixture's or an example's `package.json` doesn't make the repository "use" something; `dependencies().packages({ all: true })` lists every `package.json` when you want them.

`resolved` is the range a declaration means: for `"solid-js": "catalog:"` it is the catalog's entry, for `"@acme/ui": "workspace:^"` the local package's version. A range `semver` can't read (a git URL, `latest`) has `readable: false` and `semver.major` gives `null`.

To scope a `forEachFile` callback to files whose package uses something, ask for the file's package:

```ts theme={null}
await step(forEachFile("**/*.tsx", (file) => {
  if (!dependencies().packageOf(file.path)?.uses("solid-js", { kinds: "prod" })) return;
  // ...
}));
```

## Bump, add, remove and rename

Edits change the `package.json` files where the dependency is declared, and nothing else in them:

```ts theme={null}
await step("Update dependencies", () => {
  const deps = dependencies();
  return [
    ...deps.bump("solid-js", "^1.9.0"),
    ...deps.rename("solid-app-router", "@solidjs/router", { range: "^0.15.0" }),
    ...deps.remove("solid-testing-library"),
    // Next to solid-js, in the same section.
    ...deps.declarations("solid-js").flatMap((d) => d.package.add("@solidjs/meta", "^0.29.0", { kind: d.kind })),
  ];
});
```

Each edit returns what it changed, `{ file, package, name, kind, from, to }`, and what it left alone as `skipped`.

* A `catalog:` declaration is bumped in its catalog (`pnpm-workspace.yaml`, bun's `package.json`, or Yarn's `.yarnrc.yml`), once for every package that uses it. An entry with a YAML anchor (`react: &react ^18.2.0`) keeps it, so entries that alias it (`react-dom: *react`) follow, and are listed in the edits too.
* Peer dependencies are left alone unless `kinds` names `"peer"`: a library's `"solid-js": "^1.8.0"` peer range stays when you bump Solid. They're listed in `skipped` with `reason: "peer"`.
* A `workspace:` declaration points at one of your packages and is left alone (`reason: "workspace"`).
* A range that doesn't come from the registry, such as `file:`, `link:`, a git URL, `github:owner/repo` or a tarball URL, is left alone too (`reason: "protocol"`). An `npm:` alias keeps its target: bumping `"sw": "npm:string-width@^4.2.0"` to `^5.0.0` gives `npm:string-width@^5.0.0`.
* The root's overrides follow: when `overrides` (npm, bun), `resolutions` (Yarn, bun), `pnpm.overrides` or `pnpm-workspace.yaml`'s `overrides` pin the package by name (`"react"`, or Yarn's `"**/react"`), `bump` sets them to the new range, `remove` removes them and `rename` renames them, so an old override doesn't keep forcing the old version. A `"$react"` reference follows the declaration by itself. Overrides that only pin the package in some places (`"foo>react"`, `"react@<18"`, `"foo/react"`, npm's nested `{ "foo": { "react": ... } }`) or that point at a path are left, and listed in `skipped` with `reason: "override"`.
* `rename` to a name the package already declares removes the old declaration: the existing one keeps its range, or gets `range` when you pass it.
* `bump` takes a function to decide per declaration: `deps.bump("lodash", (d) => semver.major(d.resolved ?? "") === 3 ? "^4.17.21" : undefined)`.
* `{ kinds, in }` limit an edit to sections and packages: `deps.bump("solid-js", "^1.9.0", { kinds: ["prod", "dev"], in: "apps/web" })`. With `in`, nothing outside those packages changes: a catalog entry other packages share is left (`reason: "catalog"`), and so are the root's overrides (`reason: "override"`). To give one package its own range, `add` it there: `deps.package("apps/web").add("solid-js", "^1.9.0")` replaces its `catalog:` with the range.

The edits are the step's file changes, like a `forEachFile` callback's: with `--dry-run` they show in the diff and later steps see them, and nothing is written.

Edits change the manifests, not the lockfile, and `codemod/deps` does not run the package manager. To refresh the lockfile, run it as a [`shell`](https://docs.codemod.com/community/workflows/reference#shell-commands) step after the edits, such as `shell("pnpm install --lockfile-only")`.

## Ask the lockfile

Installed versions and the dependency graph come from the lockfile, which is read the first time you ask, not before:

```ts theme={null}
await step("Who pulls in solid-js", () => {
  const deps = dependencies();
  return {
    installed: deps.installed("solid-js"),
    chains: deps.chains({ to: "solid-js" }).map((chain) => chain.join(" > ")),
  };
});
// { installed: ["1.8.22", "1.9.5"],
//   chains: ["@demo/web@1.0.0 > solid-js@1.9.5", "@demo/ui@0.3.0 > solid-js@1.9.5"] }
```

`deps.chains({ to: deps.node("solid-js", "1.8.22") })` finds who reaches an older copy through other packages, such as `@demo/web@1.0.0 > vite-plugin-solid@2.11.6 > solid-refresh@0.6.3 > solid-js@1.8.22`. A root lockfile that can't be read is left out of the graph and listed in `deps.skipped`.

A step that edits and then asks the lockfile sees the lockfile as it was: the edit changes what is declared, not what is installed. `deps.needsInstall()` tells whether the lockfile is behind the manifests, catalog entries included: it compares them with the catalogs `pnpm-lock.yaml` or `bun.lock` recorded, or else with the installed version.

A `package.json` that can't be read (it isn't JSON) isn't one of `deps.packages()`; `deps.unreadable` lists each with why. A byte order mark at the start of a file is fine, and edits keep it.

`dependencies()` runs with the codemod CLI. The Node.js host that `codemod/testing` uses by default doesn't have it yet: test a workflow that uses it with `{ host: "quickjs" }`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.