# Fix d3.event Is Undefined After Upgrading D3

Yes. An agent upgrades D3 past version 5 in its sandbox, rewrites every handler that read the removed d3.event global, runs the UI tests and opens a PR.

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

## What the agent does

The agent clones the repository into its own cloud sandbox and finds every place the UI reads the global. In a browserify-built UI like the one in nvdv/vprof, `package.json` pins `d3` at `4.9.1` beside `browserify` 13, `jest` 18.0.0 and `uglify-es` 3.0.24; `npm run build` pipes `browserify -g browserify-css vprof/ui/main.js` into `uglifyjs`, `npm test` runs Jest over the five suites in `vprof/ui/__tests__`, and `setup.py` calls `npm run build` from its `build_ui` command. The global is read in six places: `d3.event.transform` inside a `d3.zoom().on('zoom', ...)` handler at `memory_stats.js` line 149, and `d3.event.pageX` and `d3.event.pageY` in tooltip handlers at `code_heatmap.js` lines 128 and 162, `flame_graph.js` line 207 and `profiler.js` line 103. `main.js` also uses the v4 listener signature, `(d, i, n)` with `d3.select(n[i])`, in its tab handlers at lines 36 and 55.

Before changing anything it runs the suite as it stands: `npm install` adds 772 packages and `npm test` passes 7 tests in 5 suites. It then checks what each D3 major does to this code instead of trusting a changelog: 4.9.1 and 5.16.0 expose `d3.event` as an object; 6.7.0 removes it (`'event' in d3` is false) while still shipping a CommonJS build at `dist/d3.node.js`; 7.9.2 is ESM only, with `"type": "module"` and `main` pointing at `dist/d3.mjs`. With 7 installed, every Jest 18 suite fails to load with `SyntaxError: Unexpected token 'export'` at `d3.mjs` line 2, and browserify 13 stops with `ParseError: 'import' and 'export' may appear only with 'sourceType: module'`. With 6 installed the suite still passes, and the zoom handler fails the way the title says: `TypeError: Cannot read properties of undefined (reading 'transform')`.

So the agent takes the UI to D3 6, the last major this toolchain can load, and says so in the pull request rather than forcing 7 through a bundler change nobody asked for. In 6 a listener receives the event as its first argument and the datum as its second, which it confirms by dispatching a click on a selection: the first argument is the event with `type` `click`, the second the datum. It rewrites the zoom handler to `(event) => { const t = event.transform; ... }`, the four tooltip handlers to take `event` and read `event.pageX` and `event.pageY`, and the two `(d, i, n)` tab handlers to `(event, d)` with `d3.select(event.currentTarget)`.

It also reports what it could not run. On a fresh clone `npm run build` fails before any D3 change, because `vprof/ui/css/highlight.css` begins with `@import url('node_modules/highlight.js/styles/github-gist.css')`, a path `browserify-css` resolves relative to the CSS file and cannot find. The agent leaves that import alone, notes it as a separate issue with the exact error, and proves the D3 change with the Jest suite plus a Node check that no `d3.event` reference remains in `vprof/ui`.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Upgrade d3 in this repository past version 5 and fix every use of the removed d3.event global. Read package.json, the build and test scripts, and every file under the UI directory that requires d3 first, and run the existing tests for a baseline. Check which d3 major this toolchain can load: if the bundler or test runner cannot read an ESM-only package, stop at the last major that ships a CommonJS build and explain why in the pull request. Rewrite each handler that reads d3.event to take the event as its first argument, and each (d, i, nodes) listener to the (event, datum) signature, keeping behaviour identical. If the build fails for a reason unrelated to d3, do not fix it; report the exact error separately. Run the tests until they pass and confirm with a search that no d3.event reference remains, then open a pull request listing every handler you changed with its file and line.",
  "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

What comes back is a pull request that moves `d3` to the 6.x line in `package.json`, rewrites the seven handlers in `memory_stats.js`, `code_heatmap.js`, `flame_graph.js`, `profiler.js` and `main.js`, and changes nothing else. The description lists each handler with its file and line, quotes the `TypeError` the old zoom handler throws under D3 6, and pastes the Jest run: 5 suites, 7 tests, passing.

It also says what stopped at 6: the `SyntaxError` and `ParseError` that D3 7's ESM-only build produces in Jest 18 and browserify 13, quoted from the agent's own runs, so the reviewer can decide whether a bundler upgrade is worth a second pull request. The CSS import failure in `highlight.css` is reported with its path and left untouched.

A reviewer checks the handler rewrites against the D3 6 listener signature, opens the memory chart once to see zoom still rescales the x axis, and merges. The pull request's branch and the run's full command record stay linked from the session.

## Run it on every Monday morning

An automation in the Project that holds the repository, with a `schedule` trigger, a `cron` of `0 7 * * 1` and the team's `timezone`, runs this prompt each Monday. A UI dependency that is two majors behind is exactly what a weekly run is for: one week it raises the floor to 6 and ports the handlers, a later week can take on the bundler and reach 7. A run an automation starts has its own `max_runs_per_hour` cap and is not tied to an API key.

From your own scheduler instead, send this body to `POST /o/{org}/sessions` with a `request_id` that is a UUID v5 of the week's date, so a retried job returns the run it already started. The key's `webhook_url` receives `run.completed` or `run.failed` with the session URL when the run ends.

## Why run it on Rush?

The hard part of this upgrade is not the rewrite, it is knowing where to stop. The answer depends on the bundler and the test runner in the repository, and the only honest way to find it is to install each major and run the build and the tests. The agent does that in its own sandbox, and every attempt, including the ones that fail to load, stays in the run's record, so the pull request can say "7 fails here, with this error" instead of "7 is probably too far".

A dashboard UI like this one is also the kind of code nobody opens on a laptop until it breaks. Running the upgrade on a schedule, in a sandbox with the repository checked out and the full `npm install` done fresh, turns a dependency that nobody owns into a reviewed pull request with test output attached, and the same prompt runs in every Project whose repository has a D3 UI the same way.

## Frequently asked questions

### Why stop at D3 6 instead of going to 7?

Because D3 7 ships only an ES module build, and this repository's browserify 13 and Jest 18 cannot read it: the agent observes the `ParseError` and `SyntaxError` directly and quotes them. D3 6 removes `d3.event` and changes the listener signature, which is the whole of this fix, and still ships a CommonJS build. Reaching 7 means changing the bundler or the test runner, which the pull request names as a separate piece of work.

### Does the new listener signature change what my handlers receive?

Yes. From D3 6 a listener gets the event as its first argument and the datum as its second, where v4 and v5 passed the datum, the index and the node group. The agent rewrites each handler to the new order and replaces `d3.select(n[i])` with `d3.select(event.currentTarget)`, so a handler that selected the clicked node still does.

### What if the build fails for another reason?

The agent reports it and leaves it alone. In this repository a CSS `@import` with a `node_modules` path fails under `browserify-css` on a fresh clone, before D3 is involved; the pull request quotes the error and keeps the change to D3, so the reviewer sees one upgrade, not two.

### Does the same fix work for d3.mouse and d3.touches?

Those left in D3 6 at the same time, replaced by `d3.pointer(event)`. The prompt covers `d3.event`, which is what this repository uses; a repository that also calls `d3.mouse` gets the same treatment when the prompt names it, and the agent's search for remaining references catches either.

## More D3 jobs

- [Fix "d3.scale.linear is not a function" after upgrading D3 past version 3](https://getrush.ai/run/fix-d3-scale-linear-is-not-a-function/d3/)

## More jobs like this: Upgrade dependencies weekly

- [Fix "Support for defaultProps Will Be Removed from Function Components" After a React 18.3 Upgrade](https://getrush.ai/run/fix-defaultprops-will-be-removed-from-function-components-warning/react/)
- [Fix the delim_whitespace Keyword in pd.read_csv Is Deprecated Warning](https://getrush.ai/run/fix-delim-whitespace-keyword-is-deprecated-warning/pandas/)
- [Fix "AttributeError: 'Engine' object has no attribute 'execute'" in SQLAlchemy 2.0](https://getrush.ai/run/fix-engine-object-has-no-attribute-execute/sqlalchemy/)
- [Fix "Error [ERR_REQUIRE_ESM]: require() of ES Module" After Upgrading chalk](https://getrush.ai/run/fix-err-require-esm/chalk/)
- [Fix "TypeError: expressValidator is not a function" After Upgrading express-validator](https://getrush.ai/run/fix-expressvalidator-is-not-a-function/express-validator/)

## 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/)
