# Next.js (/docs/bundlers/nextjs)

conciv runs in `next dev` through `withConciv` around your config and two instrumentation files.

<Callout type="success" title="Prefer to let your agent do it?">
  Copy this prompt into your coding agent. It runs conciv's init command, which makes the same changes as below.

  <SetupPrompt framework="nextjs" />
</Callout>

## Add conciv to Next.js [#add-conciv-to-nextjs]

Install `@conciv/it` as a dev dependency:

```package-install
npm install -D @conciv/it
```

Wrap the default export of your config with `withConciv`:

```ts twoslash title="next.config.ts"
import type {NextConfig} from 'next'
import {withConciv} from '@conciv/it/plugin/nextjs' // [!code ++]

const nextConfig: NextConfig = {
  reactStrictMode: true,
}

export default withConciv(nextConfig) // [!code ++]
```

Options go in its second argument, as in `withConciv(nextConfig, {harness: 'codex'})`. Learn more in
[Configuration](/docs/reference/configuration).

Then create both instrumentation files next to your `app` or `pages` folder:

```ts twoslash title="instrumentation.ts"
export {register} from '@conciv/it/plugin/nextjs' // <- starts the engine with the dev server
```

```ts twoslash title="instrumentation-client.ts"
import '@conciv/it/plugin/nextjs/widget' // <- adds the conciv button to your pages
```

<Callout type="warn" title="Routes in src/app or src/pages? Move both files into src/">
  Next.js reads the instrumentation files from the folder that holds `app` or `pages`. init writes them to the project
  root. With routes in src/app or src/pages, move both files into `src/`, or conciv never starts.
</Callout>

<Files>
  <File name="next.config.ts" />
  <Folder name="src" defaultOpen>
    <File name="instrumentation.ts" />
    <File name="instrumentation-client.ts" />
    <Folder name="app" />
  </Folder>
</Files>

Start your dev server with `npm run dev`, open your app, and click the conciv button. Type the 6-digit code your dev
server prints. Learn more in [Logging in](/docs/usage/logging-in).

## What works [#what-works]

- The chat, the page tools and the plugins work as on Vite.
- The [dev server tools](/docs/usage/dev-server) do not. They answer that no dev server is attached. So do plugin
  tools that read the dev server, such as the TanStack build errors.
- `next build` and `next start` leave conciv out.

<Accordions type="single">
<Accordion id="existing" title="You already have an instrumentation file">

init does not overwrite an instrumentation file that holds something else. It prints a card instead. In
`instrumentation.ts`, call conciv's `register` from your own:

```ts twoslash title="instrumentation.ts"
import {register as registerConciv} from '@conciv/it/plugin/nextjs'

export async function register() {
  await registerConciv()
}
```

</Accordion>
<Accordion id="stop" title="The engine keeps running after Ctrl+C">

Next.js gives conciv no hook to stop the engine. The engine ends with the dev server's process. When `next dev` kills its
worker fast, the agent CLI of a live run can outlive it. A larger `NEXT_EXIT_TIMEOUT_MS` gives the worker time to
exit on its own.

</Accordion>
</Accordions>

## Next steps [#next-steps]

- **[Use conciv](/docs/usage):** what the agent can do in your app.
- **[Configuration](/docs/reference/configuration):** the options `withConciv` takes.
