AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

error-tracking-node

PostHog error tracking for Node.js

Original source
posthog
License
MIT
Pinned revision
cac7c80a9e5b73484d18eda1bffb47297c938e0c

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read posthog--error-tracking-node at revision cac7c80a9e5b73484d18eda1bffb47297c938e0c, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 7

references/node.md

Node.js error tracking installation - Docs

  1. 1

    Install the package

    Required

    Install the PostHog Node.js library using your package manager:

    PostHog AI

    npm

    npm install posthog-node
    

    yarn

    yarn add posthog-node
    

    pnpm

    pnpm add posthog-node
    
  2. 2

    Initialize PostHog

    Required

    Initialize the PostHog client with your project token:

    Node.js

    PostHog AI

    import { PostHog } from 'posthog-node'
    const client = new PostHog(
        '<ph_project_token>',
        {
            host: 'https://us.i.posthog.com'
        }
    )
    
  3. 3

    Send an event

    Recommended

    Once installed, you can manually send events to test your integration:

    Node.js

    PostHog AI

    client.capture({
        distinctId: 'distinct_id_of_the_user',
        event: 'event_name',
        properties: {
            property1: 'value',
            property2: 'value',
        },
    })
    
  4. 4

    Configure exception autocapture

    Recommended

    You can enable exception autocapture when initializing the PostHog client to automatically capture uncaught exceptions and unhandled rejections in your Node app.

    Node.js

    PostHog AI

    import { PostHog } from 'posthog-node'
    const client = new PostHog(
        '<ph_project_token>',
        { 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.ts

    PostHog AI

    import express from 'express'
    import { PostHog, setupExpressErrorHandler } from 'posthog-node'
    const app = express()
    const posthog = new PostHog(POSTHOG_PROJECT_TOKEN)
    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.

  5. 5

    Manually capture exceptions

    Optional

    If you need to manually capture exceptions, you can do so by calling the captureException method:

    Node.js

    PostHog AI

    posthog.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.

  6. Verify error tracking

    Recommended

    Confirm events are being sent to PostHog

    Before proceeding, let's make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.

    Activity feed with eventsActivity feed with events

    Check for exceptions in PostHog

  7. 6

    Upload source maps

    Required

    Great, you're capturing exceptions! If you serve minified bundles, the next step is to upload source maps to generate accurate stack traces.

    Let's continue to the next section.

    Upload source maps

Community questions

Ask a question

Was this page useful?

HelpfulCould be better