Next.js error tracking

Next.js error tracking on the server and in the browser

Next.js errors happen in two places — on the server rendering your routes and in your users' browsers. ErrorLens catches both and links them with one trace.

Server: instrumentation.ts

Initialize the Node.js SDK in Next.js's instrumentation hook, and report request errors from onRequestError:

npm install @errorlenshq/node @errorlenshq/browser
instrumentation.ts
export async function register() {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    const ErrorLens = await import("@errorlenshq/node");
    ErrorLens.init({ dsn: process.env.ERRORLENS_DSN, tracesSampleRate: 0.2 });
  }
}

export async function onRequestError(error: unknown) {
  if (process.env.NEXT_RUNTIME === "nodejs") {
    const ErrorLens = await import("@errorlenshq/node");
    ErrorLens.captureException(error);
  }
}

Browser: instrumentation-client.ts

instrumentation-client.ts
import * as ErrorLens from "@errorlenshq/browser";

ErrorLens.init({
  dsn: process.env.NEXT_PUBLIC_ERRORLENS_DSN,
  tracesSampleRate: 0.1,
});
app/global-error.tsx
"use client";
import * as ErrorLens from "@errorlenshq/browser";
import { useEffect } from "react";

export default function GlobalError({ error }: { error: Error }) {
  useEffect(() => { ErrorLens.captureException(error); }, [error]);
  return <html><body><h2>Something went wrong.</h2></body></html>;
}

What you get

  • Server errors from route handlers, server components and server actions
  • Client errors with clicks, navigation and fetch breadcrumbs
  • Page loads and client-side navigations as transactions, with web vitals
  • Same-origin API calls carry traceparent, so a slow page links to the slow server request

Frequently asked questions

Does it support the Edge runtime?

The Node.js SDK runs in the Node.js runtime. For Edge routes, report errors from the browser SDK or send traces with OpenTelemetry.

Pages Router or App Router?

Both. instrumentation.ts works with either; with the Pages Router, report client errors from pages/_error or an error boundary.

Start tracking errors in minutes

Free hosted plan with 5,000 errors a month, or self-host with no limits. No credit card.