Skip to content

bun: Sentry.init() throws when @sentry/bun is loaded with require() #24580

Description

@JPeer264

With the CommonJS build of @sentry/bun, Sentry.init() throws in the default bunHttpServerIntegration:

// repro.cjs, run with `bun repro.cjs`
const Sentry = require('@sentry/bun');

Sentry.init({ dsn: 'https://public@dsn.ingest.sentry.io/1337' });
console.log('init ok'); // never printed
TypeError: undefined is not an object (evaluating 'http.default.Server')
    at instrumentBunHttpServer (packages/bun/build/cjs/integrations/bunHttpServer.js:42)
    at setupOnce (packages/bun/build/cjs/integrations/bunHttpServer.js:15)

Reproduced on Bun 1.3.14 (the CI version) and 1.4.2, on develop (10.67.0). The ESM build (import * as Sentry from '@sentry/bun') is not affected.

Cause. packages/bun/src/integrations/bunHttpServer.ts:2-3 uses default imports (import http from 'node:http', import https from 'node:https'). Rollup emits const http = require('node:http') and then reads http.default.Server (build/cjs/integrations/bunHttpServer.js:42-44). In CommonJS, Bun's node:http has no default property, so init() throws. The integration is in the default set (packages/bun/src/sdk.ts:69), so every require('@sentry/bun') user is affected, not only users of node:http servers.

Work item. Use namespace imports (import * as http from 'node:http'), or fix the CJS interop in the build. Add a regression test that loads @sentry/bun with require() and calls init(), because the existing Bun integration tests use only ESM.

How it was found. The POC that runs the Node integration suites on Bun with @sentry/node mapped to @sentry/bun (#24052) ran public-api/OnUncaughtException/basic.js, a CommonJS scenario. The error event contained this TypeError in place of the expected Error: foo. The bug came in with #22870.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugBunjavascriptPull requests that update javascript code

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions