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/browserexport 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
import * as ErrorLens from "@errorlenshq/browser";
ErrorLens.init({
dsn: process.env.NEXT_PUBLIC_ERRORLENS_DSN,
tracesSampleRate: 0.1,
});"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.
Keep reading
React error tracking, including what your error boundaries catch
Track React errors caught by error boundaries and uncaught render errors, with component stacks, breadcrumbs and source maps. Works with React 18 and 19.
Node.js error tracking with per-request context
Track Node.js errors, unhandled rejections and slow requests with per-request context and distributed tracing. Zero-dependency SDK, ESM and CommonJS.
JavaScript error tracking for real users' browsers
Track JavaScript errors in the browser with breadcrumbs, source maps, page-load timing and web vitals. 12 KB gzipped, works with any frontend. Open source.
Open-source APM that links slow requests to their errors and logs
Open-source application performance monitoring: p50/p95 latency, throughput, failure rate and distributed traces linked to errors and logs. Self-host free.
Start tracking errors in minutes
Free hosted plan with 5,000 errors a month, or self-host with no limits. No credit card.