You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/platforms/javascript/common/install/esm-without-import.mdx
+58-1Lines changed: 58 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,11 @@ supported:
17
17
[installation methods](../).
18
18
</Alert>
19
19
20
-
When running your application in ESM mode, you will most likely want to <PlatformLinkto="/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 <PlatformLinkto="/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
+
21
25
22
26
<Alertlevel='warning'title='Restrictions of this installation method'>
23
27
@@ -55,3 +59,56 @@ import http from "http";
55
59
56
60
// Your application code goes here
57
61
```
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
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.
0 commit comments