Upload source maps for Next.js

  1. Install the PostHog Next.js config package

    Required
    Terminal
    npm install @posthog/nextjs-config
  2. Add PostHog config to your Next.js app

    Required

    Add the following to your next.config.js file:

    next.config.js
    import { withPostHogConfig } from "@posthog/nextjs-config";
    const nextConfig = {
    //...your nextjs config,
    };
    export default withPostHogConfig(nextConfig, {
    personalApiKey: process.env.POSTHOG_API_KEY, // Personal API Key
    envId: process.env.POSTHOG_ENV_ID, // Environment ID
    host: process.env.NEXT_PUBLIC_POSTHOG_HOST, // (optional), defaults to https://us.posthog.com
    sourcemaps: { // (optional)
    enabled: true, // (optional) Enable sourcemaps generation and upload, default to true on production builds
    project: "my-application", // (optional) Project name, defaults to repository name
    version: "1.0.0", // (optional) Release version, defaults to current git commit
    deleteAfterUpload: true, // (optional) Delete sourcemaps after upload, defaults to true
    },
    });

    Where you should set the following environment variables:

    Environment VariableDescription
    POSTHOG_API_KEYPersonal API key with at least write access on error tracking
    POSTHOG_ENV_IDProject ID you can find in your project settings
    NEXT_PUBLIC_POSTHOG_HOSTYour PostHog instance URL. Defaults to https://us.posthog.com

    If you are using Vercel (or any other hosting service), make sure these environment variables are added to your project settings, not just your local setup. This enables source maps to be automatically uploaded on your production build.

  3. Verify source map generation

    Checkpoint

    Before proceeding, confirm that source maps are being generated by checking for .js.map files in your dist directory. These are the symbol sets that will be used to unminify stack traces in PostHog.

  4. Inject and upload source maps

    Required
    The 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.

  5. Verify source map upload and injection

    Checkpoint
    1. Confirm that source maps are successfully uploaded to PostHog.
    1. Confirm that the served files are injected with the correct source map comment in production in dev tools:
    JavaScript
    //# chunkId=0197e6db-9a73-7b91-9e80-4e1b7158db5c

Community questions

Was this page useful?

Questions about this page? or post a community question.