- 1
Install PostHog Node.js SDK
RequiredInstall the PostHog Node SDK using the package manager of your choice:
npm install posthog-node --saveIn your app, set your project API key before making any calls.
Node.jsimport { PostHog } from 'posthog-node'const client = new PostHog('<ph_project_api_key>',{ host: 'https://us.i.posthog.com' })await client.shutdown()You 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.jsimport { PostHog } from 'posthog-node'const client = new PostHog('<ph_project_api_key>',{ host: 'https://us.i.posthog.com', enableExceptionAutocapture: true })If 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.tsimport express from 'express'import { PostHog, setupExpressErrorHandler } from 'posthog-node'const app = express()const posthog = new PostHog(PH_API_KEY)setupExpressErrorHandler(posthog, app)Note: 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.jsposthog.captureException(e, 'user_distinct_id', additionalProperties)This is helpful if you've built your own error handling logic or want to capture exceptions normally handled by the framework.
- 5
Node.js error tracking installation
Questions? Ask Max AI.
It's easier than reading through 698 pages of documentation
Community questions
Was this page useful?
Next article
React error tracking installation
For Next.js, we recommend following the Next.js integration guide instead. Install posthog-js using your package manager: Add your environment variables to your .env.local file and to your hosting provider (e.g. Vercel, Netlify, AWS). You can find your project API key and host in your project settings . Including VITE_PUBLIC_ in their names ensures they are accessible in the frontend. Integrate PostHog at the root of your app (such as main.jsx if you are using Vite). You need to set…