ESM without CLI Flag

了解如何在 ESM 应用程序中运行 Sentry,而不使用 --import 标志。

当在 ESM 模式下运行应用程序时,你通常会希望 遵循 ESM 指令。然而,如果你不想使用 --import 命令行选项,例如如果你无法配置 CLI 标志,你也可以遵循另一种设置方法,直接在应用程序中导入 instrument.mjs 文件。

你需要创建一个名为 instrument.mjs 的文件,该文件导入并初始化 Sentry:

instrument.mjs
Copied
import * as Sentry from "@sentry/node";

// Ensure to call this before importing any other modules!
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",

  // Add Tracing by setting tracesSampleRate
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,
});

你需要在导入应用程序中的其他任何模块之前导入 instrument.mjs 文件。这是确保 Sentry 可以自动仪器化应用程序中所有模块的必要步骤:

app.mjs
Copied
// Import this first!
import "./instrument";

// Now import other modules
import http from "http";

// Your application code goes here