Skip to content

require-in-the-middle throws in ESM (with bun and pm2?) #18859

@The-LukeZ

Description

@The-LukeZ

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/bun

SDK Version

10.34.0

Framework Version

Link to Sentry event

https://lukez-dev.sentry.io/share/issue/7baae5a7125d46ca84971f6344104cf0/

Reproduction Example/SDK Setup

Here's a minimal reproduction for the Sentry error with require-in-the-middle in an ES module context:

Project Structure

sentry-repro/
├── package.json
├── .env (optional)
├── src/
│   ├── instrument.js
│   └── index.ts
└── pm2.config.js (optional)

Files

// package.json
{
  "name": "sentry-repro",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "dev": "bun run --preload ./src/instrument.js src/index.ts",
    "pm2": "pm2 start pm2.config.js"
  },
  "dependencies": {
    "@sentry/bun": "^10.34.0"
  },
  "devDependencies": {
    "@types/bun": "latest"
  }
}
// instrument.js
import * as Sentry from "@sentry/bun";

Sentry.init({
  dsn: "https://examplekey@o1234567.ingest.sentry.io/1234567",
  tracesSampleRate: 0.75,
});
// index.ts
import "./instrument.js";

console.log("App started successfully");

// Keep process alive
setInterval(() => {
  console.log("Still running...");
}, 5000);
// pm2.config.js (ESM syntax)
export const name = "sentry-repro";
export const script = "src/index.ts";
export const interpreter = "bun";
export const interpreter_args = "--preload ./src/instrument.js";
export const env = {
  NODE_ENV: "production",
};

Steps to Reproduce

  1. Use the code above
  2. Run the dev or pm2 script (I use pm2)
  3. Observe

Expected Result

Should throw because it's not commonjs (require only works there)

However, sentry should work with ESM

Actual Result

Does throw because it's "type": "module"

Additional Context

I'm not quite sure if the issue is because of bun and pm2, but it is most definitely because of it being an ES module and require not working in there.

However I could trace it back the dependency tree:
@sentry/bun < @sentry/node < @opentelemetry/instrumentation < require-in-the-middle

Since it is from @opentelemetry/instrumentation, maybe I should report it there as well? Or is it because of the implementation in @sentry/node ?

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions