Nuxt error tracking installation
Contents
Nuxt 3.7 and above
For Nuxt 3.7 and above, we recommend using the official @posthog/nuxt module which provides automatic error tracking with built-in sourcemap support.
- 1
Install the PostHog Nuxt module
RequiredYour goal in this step: Install and configure the PostHog Nuxt module.- Install the PostHog Nuxt module using your package manager:
- Add the module to your
nuxt.config.tsfile:
nuxt.config.tsThe module will automatically:
- Initialize PostHog on both Vue (client-side) and Nitro (server-side)
- Capture exceptions on both client and server
- Generate and upload sourcemaps during build
- 2
Manually capturing exceptions
OptionalYour goal in this step: Manually capture exceptions in your Nuxt application.Our module if set up as shown above already captures both client and server side exceptions automatically.
To send errors manually on the client side, import it and use the
captureExceptionmethod like this:VueOn the server side instantiate PostHog using:
server/api/example.js - 3
Build your project for production
RequiredBuild your project for production by running the following command:
TerminalThe PostHog module will automatically generate and upload sourcemaps to PostHog during the build process.
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.mapsource map files for//# chunkId=comments. Make sure to serve these injected files in production, PostHog will check for the//# chunkIdcomments to display the correct stack traces.
Nuxt 3.6 and below
For older versions of Nuxt, you'll need to manually set up error tracking and sourcemap uploads.
- 1
Installing PostHog Nuxt SDK
RequiredYour goal in this step: Install the PostHog Nuxt.js SDK.- Install
posthog-jsusing your package manager:
- Add your PostHog API key and host to your
nuxt.config.jsfile. You can find these in your project settings.
nuxt.config.js- Create a new plugin by creating a new file
posthog.client.jsin your plugins directory.
plugins/posthog.client.js - Install
Verify PostHog is initialized
CheckpointConfirm you can capture events with PostHogBefore proceeding, confirm that you can capture events using
posthog.capture('test_event').- 2
Manually capturing exceptions
RequiredYour goal in this step: Manually capture exceptions in your Nuxt application.To send errors directly using the PostHog client, import it and use the
captureExceptionmethod like this:VueOn the server side, you can use the
posthogobject directly.server/api/example.js - 3
Configuring exception autocapture
RequiredYour goal in this step: Enable automatic exception tracking for your Nuxt application.Update your
posthog.client.jsto add an error hookJavaScript - 5

