Node.js error tracking installation
- 1
Install PostHog Node.js SDK
RequiredInstall the PostHog Node SDK using the package manager of your choice:
In your app, set your project API key before making any calls.
Node.jsYou can find your project API key and instance address in the project settings page in PostHog.
Note: As a rule of thumb, we do not recommend hardcoding API keys. Setting it as an environment variable is preferred.
Verify PostHog is initialized
CheckpointConfirm you can capture events with PostHogBefore proceeding, confirm that you can capture events with PostHog using
client.capture('test_event')
.- 2
Configure exception autocapture
RequiredNote: A minimum SDK version of v4.5.2 is required, but we recommend keeping up to date with the latest version to ensure you have all of error tracking's features.
You can enable exception autocapture when initializing the PostHog client to automatically capture uncaught exceptions and unhandled rejections in your Node app.
Node.jsIf you are using the Express framework, you will need to import and call
setupExpressErrorHandler
with your PostHog client and Express app. This is because Express handles uncaught exceptions internally meaning exception autocapture will not work by default.server.tsNote: Error tracking requires access the file system to process stack traces. Some providers, like Cloudflare Workers, do not support Node.js runtime APIs by default and need to be included as per their documentation.
- 3
Manually capture exceptions
OptionalIf you need to manually capture exceptions, you can do so by calling the
captureException
method:Node.jsThis is helpful if you've built your own error handling logic or want to capture exceptions normally handled by the framework.
- 5