- 1
Install the PostHog Next.js config package
RequiredTerminalnpm install @posthog/nextjs-config - 2
Add PostHog config to your Next.js app
RequiredAdd the following to your
next.config.js
file:next.config.jsimport { withPostHogConfig } from "@posthog/nextjs-config";const nextConfig = {//...your nextjs config,};export default withPostHogConfig(nextConfig, {personalApiKey: process.env.POSTHOG_API_KEY, // Personal API KeyenvId: process.env.POSTHOG_ENV_ID, // Environment IDhost: process.env.NEXT_PUBLIC_POSTHOG_HOST, // (optional), defaults to https://us.posthog.comsourcemaps: { // (optional)enabled: true, // (optional) Enable sourcemaps generation and upload, default to true on production buildsproject: "my-application", // (optional) Project name, defaults to repository nameversion: "1.0.0", // (optional) Release version, defaults to current git commitdeleteAfterUpload: true, // (optional) Delete sourcemaps after upload, defaults to true},});Where you should set the following environment variables:
Environment Variable Description POSTHOG_API_KEY
Personal API key with at least write
access onerror tracking
POSTHOG_ENV_ID
Project ID you can find in your project settings NEXT_PUBLIC_POSTHOG_HOST
Your PostHog instance URL. Defaults to https://us.posthog.com
Verify source map generation
CheckpointBefore proceeding, confirm that source maps are being generated by checking for
.js.map
files in yourdist
directory. These are the symbol sets that will be used to unminify stack traces in PostHog.- 3
Inject and upload source maps
RequiredThe Next.js config package automatically handles source map injection and upload during the build process. No additional configuration is needed.Make sure you serve the injected files in production by running the same build and inject process in CI during deployment. PostHog will need access to these injected
chunkId
comments when building the stack traces.
Upload source maps for Next.js
Questions? Ask Max AI.
It's easier than reading through 698 pages of documentation
Community questions
Was this page useful?
Next article
Upload source maps for Nuxt
You can hook into the close event to generate and upload source maps for your Nuxt application like this: Build your project for production by running the following command: Post-build scripts should automatically generate and upload sourcemaps to PostHog. Before proceeding, confirm that sourcemaps are being properly uploaded. You can verify the injection is successful by checking your .mjs.map source map files for //# chunkId= comments. Make sure to serve these injected files in…