Next.js error tracking installation
Install the PostHog JavaScript library using your package manager: Add your PostHog API key and host to your Choose the integration method based on your Next.js version and router type. If you're using Next.js 15.3+, you can use For the App router, create a Then import the For the Pages router, integrate PostHog at the root of your app in The Once initialized in Use the PostHog can automatically capture unhandled exceptions in your Next.js app using the JavaScript Web SDK. You can enable exception autocapture for the JavaScript Web SDK in the Error tracking section of your project settings. It is also possible to manually capture exceptions using the Next.js uses error boundaries to handle uncaught exceptions by rendering a fallback UI instead of the crashing components. To set one up, create a You can also create a Global Error component in your root layout to capture unhandled exceptions in your root layout. For Pages Router, you can use React's Error Boundaries to catch JavaScript errors anywhere in the component tree. Create a custom error boundary component and report errors to PostHog in the Then wrap your app or specific components with the error boundary: Next.js enables you to both server-side render pages and add server-side functionality. To integrate PostHog into your Next.js app on the server-side, you can use the Node SDK. First, install the For the backend, we can create a This looks like this: You can now use the You should also see events and exceptions in PostHog coming from your server-side code in the activity feed. To capture errors that occur in your server-side code, you can set up an Importantly, you need to: This looks like this: You can find a full example of both this and client-side error tracking in our Next.js error monitoring tutorial.Install the package
RequiredAdd environment variables
Required.env.local file and to your hosting provider (e.g. Vercel, Netlify). These values need to start with NEXT_PUBLIC_ to be accessible on the client-side.Initialize PostHog
Requiredinstrumentation-client.ts for a lightweight, fast integration:providers.tsx file in your app folder. The posthog-js library needs to be initialized on the client-side using the 'use client' directive:PostHogProvider component in your app/layout.tsx and wrap your app with it:pages/_app.tsx:defaults option automatically configures PostHog with recommended settings for new projects. See SDK defaults for details.Accessing PostHog on the client
Recommendedinstrumentation-client.ts, import posthog from posthog-js anywhere and call the methods you need:usePostHog hook to access PostHog in client components:Capture client-side exceptions
RequiredcaptureException method:error.tsx file in any of your route directories. This triggers when there is an error rendering your component and should look like this:componentDidCatch method:Installing PostHog SDK for server-side
Requiredposthog-node library:lib/posthog-server.js file. In it, initialize PostHog from posthog-node as a singleton with your project API key and host from your project settings.getPostHogServer function to capture exceptions in server-side code.Verify server-side exceptions
RecommendedCapturing server-side exceptions
Requiredinstrumentation.ts file at the root of your project. This provides a onRequestError hook that you can use to capture errors.
posthog-node client in your server-side code. See our doc on setting up Next.js server-side analytics for more.nodejs runtime to ensure PostHog works. You can call posthog.debug() to get verbose logging.distinct_id from the cookie to connect the error to a specific user.

