# Migrate mkdirp to fs.mkdir with recursive: true

Yes. An agent replaces mkdirp with Node's built-in recursive mkdir, compares the two on real paths, runs your tests and removes the dependency.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-mkdirp-to-fs-mkdir/mkdirp/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the installed package before it touches a call, because the best argument for this change is inside `node_modules`. flickr/yakbak, a proxy that records HTTP responses to tape files, depends on `mkdirp` `^1.0.4` and calls it twice: `mkdirp.sync(opts.dirname)` at line 28 of `index.js`, and once in the test helper `test/helpers/tmpdir.js`. In version 1.0.4, `lib/use-native.js` checks for Node 10.12 or newer, and when it finds it `lib/mkdirp-native.js` sets `opts.recursive = true` and calls `fs.mkdirSync`. The project's CI in `.github/workflows/ci.yaml` tests Node 12, 14 and 16. On every version the project supports, the dependency is already a wrapper around the built-in call the migration would use.

It still compares the two, since a wrapper can differ at the edges. A script creates a temporary directory and runs both functions through the same cases on the sandbox's Node. They agree on a new nested path, where each returns the first directory it had to create; on an existing directory, where each returns `undefined`; on a path that is a file, `EEXIST`; on a path beneath a file, `ENOTDIR`; and on a `mode` of `0o700`. They differ in one place. Given a relative path, mkdirp returns an absolute path for the first directory created and `fs.mkdirSync` returns it as written. The agent then checks whether any caller reads the return value. In yakbak neither call does, so the difference is recorded and nothing depends on it.

One more measurement matters here because of where the call sits. Line 28 is inside the function that handles each request, so the directory check runs on every proxied call, playback included. Counting filesystem calls for a directory that already exists shows mkdirp making one `statSync` and one `mkdirSync`, and the built-in making the single `mkdirSync`. The agent replaces both call sites with `fs.mkdirSync(dirname, { recursive: true })`, spells out `recursive` in the pull request as the option that must not be dropped, since without it an existing directory throws `EEXIST`, and leaves the call where it is. Moving it out of the request path would change when a missing directory is noticed, which is a design choice for the maintainers.

Then the usual proof. `npm test` reports 16 passing before and after, and `npm run lint` is clean with the repository's ESLint config. `npm ls mkdirp` shows the package was only a direct dependency, so removing the line from `package.json` takes it out of the tree. The repository's `.npmrc` sets `package-lock=false`, which means there is no lock file to regenerate and the pull request stays small. Reading the tests, the agent notices that every case hands the middleware a directory the helper has already created, so nothing covers the reason the call exists. It adds a test that points yakbak at a nested path that does not exist and asserts the directory is there after the first request, and it runs that test against the old code as well to confirm it passes on both.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Replace the mkdirp package in this project with Node's built-in fs.mkdir and fs.mkdirSync using { recursive: true }, and remove the dependency. First install dependencies, run the tests and the linter, and record the results. Find every require or import of mkdirp in source, tests and scripts, and list each call with whether it is sync, callback or promise style, what options it passes, and whether its return value is used. Read the installed mkdirp version's own source and say whether it already delegates to the native recursive mkdir on the Node versions this project supports (check the engines field and the CI matrix). Write a script that runs mkdirp and the built-in side by side in a temporary directory and prints the result of each for: a new nested path, an existing directory, a path that is a file, a path beneath a file, a relative path, and a mode option. Report every difference and whether any call site depends on it. Convert each call, keeping the same sync or async style and error behaviour; for callback-style calls on old mkdirp versions, keep the callback contract. Do not move calls to a different point in the program's lifecycle; if a call runs on a hot path, say so and leave the decision to reviewers. Add a test for a missing nested directory if none exists, and run it against the original code too. Remove mkdirp from package.json, update the lock file if the project has one, confirm with npm ls that it is gone or name what still depends on it, and rerun tests and lint. List other small dependencies that have built-in replacements, without changing them. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request with two lines changed in `index.js`, two in `test/helpers/tmpdir.js`, one line removed from `package.json` and one new test. The description quotes the lines of mkdirp's own source that show it calling the native function, gives the side-by-side table from the comparison script with the one difference marked, and states the Node versions the project's CI covers. Test and lint results are given from before and after, and the output of `npm ls mkdirp` confirms the package is no longer installed.

A reviewer looks at three things. That `recursive: true` is present at each call. That the note about return values matches their own callers if other code imports this module. And the short list at the end, which names the remaining dependencies with built-in equivalents that the agent left alone; in the fixture that includes `rimraf` and `os-tmpdir` in the same test helper. Each of those is its own small pull request if the team wants it. If a call site turns out to rely on behaviour the built-in lacks, the agent keeps mkdirp for that site and explains why instead of forcing the change.

## Run it on every Linear issue labelled cleanup

Dependency removals are good ticket-sized work. In the Project that has the repository, create an automation with this prompt and a `linear` trigger on `linear.issue.labeled`, filtered to the label your team uses for such chores. When a member of your org adds the label, a run starts with the issue's text placed after the prompt, so the issue can name the package to remove while the prompt carries the method. The trigger uses the Linear connection of the member the automation runs as.

Keep one package per issue. The comparison script and the reading of the package's source are specific to each dependency, and a pull request that removes one thing is quick to review and easy to revert. Several labelled issues start several runs, up to the automation's max runs per hour.

## Why run it on Rush?

Removing a dependency is a claim that nothing changes, and backing it needs a clean install, the test suite, the linter and a scratch script exercising both implementations on a real filesystem. A Rush run does all of that in its own sandbox from an empty checkout, which also shows what a fresh `npm install` produces without the package. The commands and their output are kept in the session, so the table in the pull request can be traced to the run that printed it.

The work is also the kind that waits forever in a backlog: nobody is blocked by an extra package until an audit flags it or an upgrade breaks. Handing a labelled issue to an agent turns it into a reviewed pull request without taking a developer off other work, and because each run is isolated, several such removals can proceed at once without sharing a working tree.

## Frequently asked questions

### Is fs.mkdir with recursive: true the same as mkdirp?

For the common cases, yes, and on the fixture's version mkdirp calls it internally. Both create missing parents, both accept an existing directory, and both raise `EEXIST` when the path is a file. The difference the comparison finds is the return value for a relative path, which mkdirp makes absolute.

### Which Node version do I need?

mkdirp 1.0.4 itself treats Node 10.12 as the first version with the native recursive option, in its `use-native.js`. Any project whose supported versions are newer than that can use the built-in. The agent checks your `engines` field and CI matrix before converting.

### What about the old callback API, mkdirp(dir, cb)?

Releases before 1.0 took a callback and newer ones return a promise. The agent converts each style to its equivalent, `fs.mkdir` with a callback or `fs.promises.mkdir`, and keeps the surrounding control flow, so a callback-based caller is not rewritten to async functions as a side effect.

### Will mkdirp disappear from node_modules?

Only if nothing else depends on it. The run checks with `npm ls mkdirp` after the change. In the fixture it is a direct dependency only, so removing the line takes it out; in a larger project a tool may still pull it in, and the pull request names which one.

## More jobs like this: Turn a Linear label into a PR

- [Replace Moment.js With Day.js in an Express API](https://getrush.ai/run/migrate-moment-to-dayjs/moment/)
- [Replace pytz With zoneinfo in a Python Scraper](https://getrush.ai/run/migrate-pytz-to-zoneinfo/pytz/)
- [Migrate From querystring to URLSearchParams in a Node.js API Client](https://getrush.ai/run/migrate-querystring-to-urlsearchparams/querystring/)
- [Migrate From Requests to HTTPX in a Python Project](https://getrush.ai/run/migrate-requests-to-httpx/requests/)
- [Replace shortid With nanoid in a React App](https://getrush.ai/run/migrate-shortid-to-nanoid/shortid/)
- [Migrate a Blazor App from SQL Server to PostgreSQL](https://getrush.ai/run/migrate-sql-server-to-postgres/blazor/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
