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.
With the CommonJS build of
@sentry/bun,Sentry.init()throws in the defaultbunHttpServerIntegration: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-3uses default imports (import http from 'node:http',import https from 'node:https'). Rollup emitsconst http = require('node:http')and then readshttp.default.Server(build/cjs/integrations/bunHttpServer.js:42-44). In CommonJS, Bun'snode:httphas nodefaultproperty, soinit()throws. The integration is in the default set (packages/bun/src/sdk.ts:69), so everyrequire('@sentry/bun')user is affected, not only users ofnode:httpservers.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/bunwithrequire()and callsinit(), 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/nodemapped to@sentry/bun(#24052) ranpublic-api/OnUncaughtException/basic.js, a CommonJS scenario. The error event contained thisTypeErrorin place of the expectedError: foo. The bug came in with #22870.