- 1
Install the PostHog CLI
RequiredInstall
posthog-cli
:npm install -g @posthog/cli - 2
Authenticate the PostHog CLI
RequiredTo authenticate the CLI, you can call the
login
command and follow the instructions:Terminalposthog-cli loginIf you are using the CLI in a CI/CD environment such as GitHub Actions, you can set environment variables to authenticate:
Environment Variable Description Source POSTHOG_CLI_ENV_ID
PostHog project ID Environment settings POSTHOG_CLI_TOKEN
Personal API key with error tracking write
scopeAPI key settings Use the
--host
option in subsequent commands to specify a different PostHog instance / or region. For EU users:Terminalposthog-cli --host https://eu.posthog.com [CMD] - 3
Generate sourcemaps during build
RequiredYou can hook into the
close
event to generate and upload source maps for your Nuxt application like this:nuxt.config.jsimport { execSync } from 'child_process'export default defineNuxtConfig({...,sourcemap: {client: true},hooks: {'close': async () => {console.log('Running PostHog sourcemap injection and upload...')try {execSync("posthog-cli sourcemap inject --directory '.output'", {stdio: 'inherit',})execSync("posthog-cli sourcemap upload --directory '.output'", {stdio: 'inherit',})console.log('PostHog sourcemap injection completed successfully')} catch (error) {console.error('PostHog sourcemap injection failed:', error)}}}}) - 4
Build your project for production
RequiredBuild your project for production by running the following command:
Terminalnuxt buildPost-build scripts should automatically generate and upload sourcemaps to PostHog.
Verify source map upload
CheckpointConfirm sourcemaps are being properly uploadedBefore 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 production, PostHog will check for the//# chunkId
comments to display the correct stack traces.- 5
Next steps
After configuring PostHog, update your build and deployment process to serve bundles injected with the
chunkId
comments. PostHog relies on these comments to display the correct stack traces. Remember to export the required environment variables in your deployment process.
Upload source maps for Nuxt
Questions? Ask Max AI.
It's easier than reading through 698 pages of documentation