Solution skill
Sentry Node.js ESM vs CJS: match the instrument file to the module system
Complete solution
ESM and CommonJS need different instrument files and preload flags. Pick the wrong pair and the SDK loads too late or not at all.
TL;DR: ESM and CommonJS need different instrument files and preload flags. Pick the wrong pair and the SDK loads too late or not at all. Move it earlier via the preload flag.
The pairs
CommonJS:
// instrument.js
const Sentry = require("@sentry/node");
Sentry.init({ dsn: "___PUBLIC_DSN___" });node --import ./instrument.js app.jsESM ("type": "module" in package.json):
// instrument.mjs
import * as Sentry from "@sentry/node";
// Ensure to call this before importing any other modules!
Sentry.init({ dsn: "___PUBLIC_DSN___" });node --import ./instrument.mjs app.mjsHow this breaks
- ESM app with
instrument.js(CJS): therequirecall fails or the file loads in the wrong context, and init never runs. No errors in Sentry, obviously, because Sentry is the thing that is broken. - Forgetting
--importand instead importing instrument from app code: by the time your import runs, Express, Prisma, and everything else already loaded without instrumentation. You get errors but no traces, and the gap is confusing. - TypeScript projects: the instrument file must be compiled or run through your TS loader with the same
--importregistration order.ts-nodeusers need the loader flag pointing at the instrument file, not the app entry.
The check
If error events arrive but spans are missing for libraries you expect (http, pg, redis), init ran but too late. Move it earlier via the preload flag. If nothing arrives at all, the file pair is wrong or the flag is missing from the start command (check the Dockerfile CMD and the process manager config, not just package.json scripts).
When to use this
- This covers exactly what the title says: Sentry Node.js ESM vs CJS.
- You are setting this up for the first time, or auditing an existing setup.
- You want the key gotchas in one place before you start.
When not to use this
- You are doing a different workflow with Sentry; these steps are specific to the title above.
- You need the full reference docs; this is the short path, not the manual.
Compatibility
- Not pinned to a specific version; follows current Sentry behavior.
Evidence
Researched against live Sentry docs 2026-09-26. The Node.js guide (docs.sentry.io/platforms/javascript/guides/node/) documents creating instrument.(js|mjs), the ESM comment Ensure to call this before importing any other modules, and the two preload invocations: node --import ./instrument.js app.js for CommonJS and node --import ./instrument.mjs app.mjs for ESM. It also notes bundler users should follow the bundler setup docs instead.
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_K0DQH29ZEwCTEyRFgIJuSA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Sentry Node.js ESM vs CJS: match the instrument file to the module system <span>— Vectle</span></a>