Skip to content

Commit a399e4d

Browse files
authored
feat(node): Document SEA setup (#17752)
A user asked how to get their instrumentations working in a [Node.js Single Applications](https://nodejs.org/api/single-executable-applications.html) setup, I tried a few things and settled on this as it seems to work best and simplest way to set it up. Not sure if needs its own dedicated page, but the user did follow the trail of not being able to use `--import`, so it might be an appropriate place for it.
1 parent 86bab1b commit a399e4d

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

docs/platforms/javascript/common/install/esm-without-import.mdx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ supported:
1717
[installation methods](../).
1818
</Alert>
1919

20-
When running your application in ESM mode, you will most likely want to <PlatformLink to="/install/esm">follow the ESM instructions</PlatformLink>. However, if you want to avoid using the `--import` command line option, for example if you have no way of configuring a CLI flag, you can also follow an alternative setup that involves importing the `instrument.mjs` file directly in your application.
20+
21+
When running your application in ESM mode, you will most likely want to <PlatformLink to="/install/esm">follow the ESM instructions</PlatformLink>. However, if you can't use the `--import` command line option, you can either use [direct imports](#direct-imports) or [SEA bootstrap setup](#nodejs-single-executable-applications) if you are using a Node.js Single Executable Application (SEA).
22+
23+
## Direct Imports
24+
2125

2226
<Alert level='warning' title='Restrictions of this installation method'>
2327

@@ -55,3 +59,56 @@ import http from "http";
5559

5660
// Your application code goes here
5761
```
62+
63+
## Node.js Single Executable Applications
64+
65+
Node.js Single Executable Applications (SEA) may not load your Sentry instrumentation early enough, so you need to package a small bootstrap file as the SEA main instead of packaging your app entrypoint directly.
66+
67+
The embedded SEA main should only load a filesystem bootstrap file next to the
68+
executable:
69+
70+
```javascript {filename: sea-main.cjs}
71+
const { createRequire } = require("node:module");
72+
73+
createRequire(__filename)("./sea-bootstrap.cjs");
74+
```
75+
76+
The filesystem bootstrap imports Sentry first, then imports your real app
77+
entrypoint:
78+
79+
```javascript {filename: sea-bootstrap.cjs}
80+
async function startApp() {
81+
await import("./instrument.mjs");
82+
await import("./app.mjs");
83+
}
84+
85+
startApp();
86+
```
87+
88+
Keep your Sentry setup in `instrument.mjs`:
89+
90+
```javascript {tabTitle:ESM} {filename: instrument.mjs}
91+
import * as Sentry from "@sentry/node";
92+
93+
Sentry.init({
94+
dsn: "___PUBLIC_DSN___",
95+
tracesSampleRate: 1.0,
96+
});
97+
```
98+
99+
Then configure SEA to use `sea-main.cjs` as its main script:
100+
101+
```json {filename: sea-config.json}
102+
{
103+
"main": "sea-main.cjs",
104+
"output": "sea-prep.blob",
105+
"disableExperimentalSEAWarning": true,
106+
"useSnapshot": false
107+
}
108+
```
109+
110+
Keep `sea-bootstrap.cjs`, `instrument.mjs`, and `app.mjs` available on the filesystem next to the executable.
111+
112+
This setup lets the Sentry SDK register ESM instrumentation hooks before your application imports instrumented modules, such as Express or database clients. Your instrumentation file and app entrypoint can stay ESM. The verified bootstrap pattern shown here uses CommonJS only for the small SEA entry files.
113+
114+
Node.js SEA support is still evolving, including how embedded ESM entrypoints and module loading are configured. The embedded SEA main may not be able to load filesystem modules with `import()` directly, so the example above uses `module.createRequire()` to bridge from the embedded main to a normal filesystem bootstrap. The important requirement is startup order: load Sentry before loading the application modules you want Sentry to instrument.

docs/platforms/javascript/common/install/esm.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ If it is not possible for you to pass the `--import` flag to the Node.js binary,
4747
NODE_OPTIONS="--import ./instrument.mjs" npm run start
4848
```
4949

50+
If you're building a Node.js Single Executable Application (SEA) and can't rely
51+
on `--import` or `NODE_OPTIONS`, use the <PlatformLink to="/install/esm-without-import/#nodejs-single-executable-applications">SEA
52+
bootstrap setup</PlatformLink> instead.
53+
5054
We do not support ESM in Node versions before 18.19.0.
5155

5256
## Troubleshooting instrumentation

0 commit comments

Comments
 (0)