ObserverKit

Next.js

Track production errors in a Next.js App Router app: initialize in instrumentation-client.ts, report render errors from app/error.tsx, and upload source maps at build time with @observerkit/next.

1. Install the SDK

Install the package

terminal
pnpm add @observerkit/js

Initialize

Call init as early as possible in your application entry point. Uncaught errors and unhandled promise rejections are captured automatically.

instrumentation-client.ts
import { init } from "@observerkit/js"

init({
  projectKey: "ok_proj_...",
  environment: "production",
})

Next.js 15.3+ runs instrumentation-client.ts before your app code on the client, so errors are captured from the very first render.

Options

projectKeystringRequired. Your project key.
autoCapturebooleanCapture uncaught errors automatically. Defaults to true.
environmentstringEnvironment name, e.g. "production" or "staging".
endpointURLCustom ingest API URL.
servicestringOptional. Distinguishes services within one project, e.g. multiple apps in a monorepo. Auto-created on first event, up to 20 per project.

Capture errors manually

Errors are captured automatically by default. You can also report them manually with additional metadata.

app/error.tsx
"use client"

import { useEffect } from "react"
import { captureException } from "@observerkit/js"

export default function ErrorBoundary({
  error,
  reset,
}: {
  error: Error
  reset: () => void
}) {
  useEffect(() => {
    captureException(error)
  }, [error])

  return (
    <div>
      <p>Something went wrong.</p>
      <button onClick={reset}>Try again</button>
    </div>
  )
}

Route error boundaries catch render errors the App Router would otherwise swallow. The try/catch pattern with captureException also works in any client code.

Verify

Trigger a test error in your application. If everything is set up correctly, it will appear in your dashboard within a few seconds.

<button
  onClick={() => {
    throw new Error("test error")
  }}
>
  Throw test error
</button>

2. Upload source maps

Install the bundler plugin

terminal
pnpm add -D @observerkit/next

The plugin uploads source maps on each build and injects debug IDs for accurate stack trace mapping.

Configure

Add the plugin to your Vite config. Source maps are enabled automatically.

next.config.ts
import withObserverkit from "@observerkit/next"
import type { NextConfig } from "next"

const nextConfig: NextConfig = {
  // your existing Next.js config
}

export default withObserverkit(nextConfig, {
  projectKey: process.env["OBSERVERKIT_PROJECT_KEY"] ?? "",
})

Requires Next.js 15.4.1 or later. Works in both webpack and Turbopack modes. Client and server source maps are uploaded after next build. When the project key is unset (local dev, keyless CI), the plugin is disabled and your config passes through untouched.

Options

projectKeystringRequired. Your project key.
endpointstringIngest API URL. Defaults to ingest.observerkit.com.
deleteAfterUploadbooleanRemove .map files from output after upload and strip sourceMappingURL references so nothing dangles. Defaults to true.

Verify the build uploaded

A production build prints one upload summary per output directory. A framework that builds several environments prints one per environment.

[ObserverKit] Enabling build.sourcemap for source map upload
[ObserverKit] Uploaded 12 source map(s) from /app/build/client
[ObserverKit] Deleted 12 .map files from output
[ObserverKit] Uploaded 3 source map(s) from /app/build/server
[ObserverKit] Deleted 3 .map files from output

If you see Output directory not found or No .map files found in, the plugin scanned somewhere your build does not write to. The message names the path it tried. Transient upload failures are retried twice and log each attempt, so a slow build is explicable rather than mysterious.

No .map file should remain in your output afterwards. With deleteAfterUpload left on, running find build -name "*.map" should print nothing.

You do not have to catch this in a build log. If your app sends errors referencing a build whose source maps never arrived, your project settings page says so, along with the date of your last successful upload. A project that has never uploaded and one that stopped uploading are called out differently, because they point at different causes.

Verify a stack trace resolves

Drop a button into your app that throws, trigger it after a production build, then check your dashboard. The stack trace should show original file names and line numbers.

<button
  onClick={() => {
    throw new Error("test error")
  }}
>
  Throw test error
</button>