Skip to content

ESM Mode breaks when using openai package #12237

Closed
@danilofuchs

Description

@danilofuchs

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

8.4.0

"openai": "^4.33.1"

Framework Version

Node 8.4.0

Link to Sentry event

No response

SDK Setup

// instrumentation.ts
Sentry.init({
  dsn: "",

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  // We recommend adjusting this value in production
  tracesSampleRate: process.env.NODE_ENV === "prod" ? 0.2 : 1,
  profilesSampleRate: 1, // Relative to tracesSampleRate

  environment: process.env.NODE_ENV,
  enabled: process.env.NODE_ENV === "prod",
});

Steps to Reproduce

https://github.com/danilofuchs/sentry-esm-openai-repro

  1. Add openai package
  2. Instrument using instrumentation.js file
  3. Run with node --import ./build/instrumentation.js build/server.js

Expected Result

Runs with Sentry instrumentation.

Perhaps related to #12059 and #12154 (import-in-the-middle bugs)

Actual Result

> node --import ./build/instrumentation.js build/server.js

file:///home/project/node_modules/openai/resources/beta/assistants/files.mjs:51
    Files.AssistantFilesPage = FilesAPI.AssistantFilesPage;
                                        ^

ReferenceError: Cannot access 'AssistantFilesPage' before initialization
    at file:///project/node_modules/openai/resources/beta/assistants/files.mjs:51:41
    at file:///home/project/node_modules/openai/resources/beta/assistants/files.mjs:52:3
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.12.2

files.mjs:51:41:

export class AssistantFilesPage extends CursorPage {
}
(function (Files) {
    Files.AssistantFilesPage = FilesAPI.AssistantFilesPage;
})(Files || (Files = {}));
//# sourceMappingURL=files.mjs.map

Obs:
Setting globalThis._sentryEsmLoaderHookRegistered = true; before Sentry.init fixes the crash but disables auto instrumentation

Metadata

Metadata

Assignees

Labels

Package: nodeIssues related to the Sentry Node SDK

Type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions