# axle prune

Write a smaller copy of the repository with only the packages an app needs, for Docker.

```sh
axle prune <package>... [--out <folder>] [--docker] [--production]
```

A Docker image of one app doesn't need the rest of the monorepo, but copying all of it means any change anywhere misses the image's cache. `axle prune` writes a copy with only the packages you name, the workspace packages they depend on and a lockfile trimmed to match:

```text
Wrote @acme/ui, @acme/utils, @acme/web into out: 214 files.
```

## What the copy holds

- The packages you name, and every workspace package they depend on, directly or through others, as [`axle graph`](https://axlers.dev/docs/reference/graph.md) shows them. Each keeps every file Git tracks in its folder, and new files that `.gitignore` doesn't ignore, so `node_modules` and build output stay out. A package inside another one's folder is left out unless it's kept too.
- The files at the repository's root, such as `package.json`, `pnpm-workspace.yaml`, `.npmrc`, `axle.toml` and `tsconfig.json`, but not the folders there, other than `patches` and `.yarn` without its cache and install state.
- The lockfile, with only the entries the kept packages and the root use. `pnpm-lock.yaml`, `aube-lock.yaml`, `nub.lock`, `package-lock.json`, `yarn.lock` and `bun.lock` are trimmed; Bun's older binary `bun.lockb` stops the prune with [`prune-failed`](https://axlers.dev/docs/messages/prune-failed.md), and Bun converts it with `bun install --save-text-lockfile`.

A symbolic link to a file the copy could hold anyway, one inside the repository that `.gitignore` doesn't ignore, is copied as that file. Other links, links to folders and Git submodules are left out, so no file outside the repository or ignored by Git reaches the copy. A dependency on a folder that isn't a workspace package, such as `"foo": "file:../vendor/foo"`, isn't copied either: make that folder a workspace package.

Installing in the copy with a frozen lockfile, such as `pnpm install --frozen-lockfile`, `npm ci`, `yarn install --immutable` (`--frozen-lockfile` with Yarn 1), `bun install --frozen-lockfile`, `nub ci` or `aube ci`, works without changing the lockfile. The workspace's patterns, such as `packages/*`, stay as they are and match only the folders that are there.

`--out` is a new or empty folder, `out` by default, relative to the current folder. It can be inside the repository, where `.gitignore` should list it, but can't be the repository, a folder around it or a folder inside `.git`. If writing fails, Axle removes what it wrote.

## For Docker

`--docker` splits the copy in two, so installing comes from Docker's cache until a dependency changes:

- `out/json` holds what installing needs: every `package.json`, the lockfile, `pnpm-workspace.yaml`, `aube-workspace.yaml`, `nub.jsonc`, `.npmrc`, `.pnpmfile.cjs`, `.yarnrc`, `.yarnrc.yml`, `bunfig.toml`, `patches` and `.yarn`.
- `out/full` holds the whole copy.

With `--docker`, Axle ends with `Wrote @acme/ui, @acme/utils, @acme/web into out: what installing needs in json, and all 214 files in full.`

Copy `out/json` and install, then copy `out/full` over it and build:

**pnpm**

```dockerfile
FROM node:24-slim AS base
RUN corepack enable

FROM base AS prune
WORKDIR /repo
COPY . .
RUN pnpm dlx @axle-rs/axle prune @acme/web --docker

FROM base AS build
WORKDIR /repo
COPY --from=prune /repo/out/json/ .
RUN pnpm install --frozen-lockfile
COPY --from=prune /repo/out/full/ .
RUN pnpm exec axle run build --filter @acme/web
```

**npm**

```dockerfile
FROM node:24-slim AS prune
WORKDIR /repo
COPY . .
RUN npx @axle-rs/axle prune @acme/web --docker

FROM node:24-slim AS build
WORKDIR /repo
COPY --from=prune /repo/out/json/ .
RUN npm ci
COPY --from=prune /repo/out/full/ .
RUN npx axle run build --filter @acme/web
```

**Yarn**

```dockerfile
FROM node:24-slim AS base
RUN corepack enable

FROM base AS prune
WORKDIR /repo
COPY . .
RUN yarn dlx @axle-rs/axle prune @acme/web --docker

FROM base AS build
WORKDIR /repo
COPY --from=prune /repo/out/json/ .
RUN yarn install --immutable
COPY --from=prune /repo/out/full/ .
RUN yarn axle run build --filter @acme/web
```

With Yarn 1, run Axle in the prune stage with `npx` instead of `yarn dlx`, and install with `yarn install --frozen-lockfile`.

**Bun**

```dockerfile
FROM oven/bun:1 AS prune
WORKDIR /repo
COPY . .
RUN bunx @axle-rs/axle prune @acme/web --docker

FROM oven/bun:1 AS build
WORKDIR /repo
COPY --from=prune /repo/out/json/ .
RUN bun install --frozen-lockfile
COPY --from=prune /repo/out/full/ .
RUN bunx axle run build --filter @acme/web
```

**nub**

```dockerfile
FROM node:24-slim AS base
RUN npm install --global @nubjs/nub

FROM base AS prune
WORKDIR /repo
COPY . .
RUN nub dlx @axle-rs/axle prune @acme/web --docker

FROM base AS build
WORKDIR /repo
COPY --from=prune /repo/out/json/ .
RUN nub ci
COPY --from=prune /repo/out/full/ .
RUN nubx axle run build --filter @acme/web
```

**aube**

```dockerfile
FROM node:24-slim AS base
RUN npm install --global --ignore-scripts=false @endevco/aube

FROM base AS prune
WORKDIR /repo
COPY . .
RUN aube dlx @axle-rs/axle prune @acme/web --docker

FROM base AS build
WORKDIR /repo
COPY --from=prune /repo/out/json/ .
RUN aube ci
COPY --from=prune /repo/out/full/ .
RUN aube exec axle run build --filter @acme/web
```

The build stage runs Axle from the copy's own dependencies, so list `@axle-rs/axle` in the root `package.json`'s `devDependencies` and prune that stage without [`--production`](#without-devdependencies), and give the prune stage the same version, such as `@axle-rs/axle@0.1.0`. Keep `node_modules`, build output and `out` out of Docker's context with a `.dockerignore`. When the context leaves `.git` out, or the image lacks Git, as `node:24-slim` and `oven/bun` do, Axle picks the files by `.gitignore` alone. The examples use Node.js 24 because it still ships Corepack; from Node.js 25 on, install it first with `npm install --global corepack`.

## Without devDependencies

An image that only runs the app doesn't need its build and test tools. `--production` leaves `devDependencies` out:

- Each `package.json` in the copy, the root's included, loses its `devDependencies`. A package listed there and also under `dependencies` or `optionalDependencies` stays.
- A workspace package reached only through `devDependencies` isn't kept. A dependency in [`axle.toml`](https://axlers.dev/docs/reference/configuration.md) still keeps the package it names, unless that package is one of the `devDependencies`.
- The lockfile keeps only what the kept packages and the root need without their `devDependencies`, and their own entries lose them too, so it matches the copy's `package.json` files.

Installing in the copy with a frozen lockfile and the package manager's production flag works without changing the lockfile:

**pnpm**

```sh
pnpm install --frozen-lockfile --prod
```

**npm**

```sh
npm ci --omit=dev
```

**Yarn**

```sh
yarn workspaces focus --all --production
```

With Yarn 1, `yarn install --frozen-lockfile --production`.

**Bun**

```sh
bun install --frozen-lockfile --production
```

**nub**

```sh
nub install --frozen-lockfile --prod
```

**aube**

```sh
aube install --frozen-lockfile --prod
```

Since the `devDependencies` are gone, a plain frozen install, such as `pnpm install --frozen-lockfile` or `yarn install --immutable`, installs the same. The copy can't build what needs those tools, so build in a stage pruned without `--production`, and copy the build output into a stage pruned with it:

```text
Wrote @acme/utils, @acme/web into out: 112 files, without devDependencies.
```

## Deploying only what changed

Hosting services such as Vercel and Netlify run a command to decide whether to build a project, and skip the build when it exits with 0. [`axle affected --exit-code`](https://axlers.dev/docs/reference/affected.md#deciding-whether-to-deploy) exits with 1 when the app is affected and 0 when it isn't.

## Options

| Option           | What it does                                                            |
| ---------------- | ----------------------------------------------------------------------- |
| `--out <folder>` | the new or empty folder to write into, `out` by default                 |
| `--docker`       | writes what installing needs into `json` and the whole copy into `full` |
| `--production`   | leaves `devDependencies` out, and the lockfile entries only they need   |

Each problem has its own page: [`prune-folder-not-empty`](https://axlers.dev/docs/messages/prune-folder-not-empty.md), [`prune-failed`](https://axlers.dev/docs/messages/prune-failed.md), [`unknown-package`](https://axlers.dev/docs/messages/unknown-package.md), [`could-not-read`](https://axlers.dev/docs/messages/could-not-read.md) and [`file-not-written`](https://axlers.dev/docs/messages/file-not-written.md).
