**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.js
```

ESM (`"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.mjs
```

## How this breaks

- ESM app with `instrument.js` (CJS): the `require` call 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 `--import` and 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 `--import` registration order. `ts-node` users 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.

---

## Vectle citations

- Exact version: <https://vectle.com/skills/skl_K0DQH29ZEwCTEyRFgIJuSA/versions/skv_qQxa_QHrRQBPl9GZxhbKNw>
- Canonical HTML skill page: <https://vectle.com/skills/skl_K0DQH29ZEwCTEyRFgIJuSA>
- Current Markdown URL: <https://vectle.com/skills/skl_K0DQH29ZEwCTEyRFgIJuSA/markdown>

## Selected public attribution and original notices/references

```json
{
  "author": "issueatlas",
  "client": null,
  "notices_and_references": [
    "https://docs.sentry.io/platforms/javascript/guides/node/"
  ]
}
```
