Skip to content

Commit a4e6865

Browse files
andreiborzavivianyentranLuca Forstnerstephanie-anderson
authored andcommitted
add instruction on node --require (#9932)
Co-authored-by: vivianyentran <[email protected]> Co-authored-by: Luca Forstner <[email protected]> Co-authored-by: Stephanie Anderson <[email protected]>
1 parent 017c2a6 commit a4e6865

File tree

28 files changed

+841
-197
lines changed

28 files changed

+841
-197
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: Initialization Methods
3+
description: "Review our alternate ways to initialize and run Sentry."
4+
supported:
5+
- javascript.node
6+
- javascript.koa
7+
- javascript.connect
8+
- javascript.fastify
9+
- javascript.express
10+
---
11+
12+
The Sentry Node.js SDK can be initialized in multiple ways. Read this page to figure out which one to use.
13+
14+
## How To Decide Which Method To Use
15+
16+
You can initialize the SDK by either
17+
18+
- Using the `--require` (CJS) or `--import` (ESM) flags to import a dedicated file to initialize the SDK (recommended)
19+
- Importing a dedicated file to initialize Sentry from within your application
20+
21+
### `--require` Or `--import`
22+
23+
Using [--require](https://nodejs.org/api/cli.html#-r---require-module) or [--import](https://nodejs.org/api/cli.html#--importmodule) guarantees that Sentry is imported and initialized before any other modules in your application, which is necessary for the SDK to work properly.
24+
25+
```bash
26+
# If you are using CommonJS (CJS)
27+
node --require ./instrument.js app.js
28+
29+
# If you are using ECMAScript Modules (ESM)
30+
# Note: This is only available for Node v18.19.0 onwards.
31+
node --import ./instrument.mjs app.mjs
32+
```
33+
34+
In some situations however, it may not be possible for you to use these flags:
35+
36+
- **No access to Node.js arguments**: There are many ways of running Node.js and it may not be possible for you to change the runtime flags due to technical or organizational limitations.
37+
- **Old Node.js versions**: If you are using ECMAScript modules (ESM), you must load your dedicated Sentry initialization file with the `--import` flag. This flag was only introduced in Node.js v18.18.0 and onwards.
38+
39+
If you find yourself in one of these situations, you can evaluate simply importing your Sentry initialization file with an import statement inside your application instead.
40+
41+
### Importing The Sentry Initialization File Directly
42+
43+
As an alternative to the `--require` or `--import` flags, you can simply import your Sentry initialization file directly from your application.
44+
**Please note that this is not recommended** since `Sentry.init()` should be run before any `import` or `require` statements.
45+
The Sentry SDK depends on import ordering for instrumenting native and third-party modules.
46+
We discourage directly importing the initialization file since import ordering is error-prone or can be flagged and mistakenly auto-fixed by linters or changed by code formatters.
47+
48+
```javascript {tabTitle:CommonJS}
49+
// Ensure to require this before requiring any other modules!
50+
require("./instrument.js");
51+
52+
// http and other libraries are only instrumented
53+
// if required _after_ Sentry has been initialized
54+
const http = require("http");
55+
```
56+
57+
```javascript {tabTitle:ESM}
58+
// Ensure to import this before importing any other modules!
59+
import "./instrument.js";
60+
61+
// http and other libraries are only instrumented
62+
// if imported _after_ Sentry has been initialized
63+
import http from "http";
64+
```
65+
66+
If you are using ESM, you additionally need to pass a `--loader` flag, registering the SDKs ESM module loader, so that the SDK can properly instrument libraries:
67+
68+
```bash
69+
node --loader @sentry/node/loader app.mjs
70+
```

docs/platforms/javascript/guides/connect/index.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ yarn add @sentry/node
3030

3131
## Configure
3232

33-
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your application - otherwise, auto-instrumentation of these modules will _not_ work.
33+
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your applicationotherwise, auto-instrumentation of these modules will _not_ work.
3434

3535
Once this is done, Sentry's Node SDK captures unhandled exceptions as well as tracing data for your application.
3636

37+
We recommend creating an `instrument.js` file that imports and initializes Sentry.
38+
3739
<PlatformContent includePath="getting-started-config" />
3840

3941
If you set a `tracesSampleRate`, performance instrumentation will automatically be enabled for you. See <PlatformLink to="/performance/instrumentation/automatic-instrumentation">Automatic Instrumentation</PlatformLink> to learn about all the things that the SDK automatically instruments for you.
4042

41-
You can also manually capture performance data - see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
43+
You can also manually capture performance data—see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
44+
45+
## Use
46+
47+
In your Connect application, import Sentry and set up the error handler.
48+
49+
<PlatformContent includePath="getting-started-use" />
50+
51+
## Run
52+
53+
Adjust the Node.js call for your application to use the [--require](https://nodejs.org/api/cli.html#-r---require-module) or [--import](https://nodejs.org/api/cli.html#--importmodule) parameter and point it at `instrument.js`
54+
55+
<PlatformContent includePath="getting-started-run" />
4256

43-
<Include name="node-esm-compatibility" />
57+
<Include name="node-esm-compatibility" />
4458

4559
<PlatformContent includePath="getting-started-sourcemaps" />
4660

docs/platforms/javascript/guides/express/index.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ yarn add @sentry/node
3030

3131
## Configure
3232

33-
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your application - otherwise, auto-instrumentation of these modules will _not_ work.
33+
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your applicationotherwise, auto-instrumentation of these modules will _not_ work.
3434

3535
Once this is done, Sentry's Node SDK captures unhandled exceptions as well as tracing data for your application.
3636

37+
We recommend creating an `instrument.js` file that imports and initializes Sentry.
38+
3739
<PlatformContent includePath="getting-started-config" />
3840

3941
If you set a `tracesSampleRate`, performance instrumentation will automatically be enabled for you. See <PlatformLink to="/performance/instrumentation/automatic-instrumentation">Automatic Instrumentation</PlatformLink> to learn about all the things that the SDK automatically instruments for you.
4042

41-
You can also manually capture performance data - see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
43+
You can also manually capture performance data—see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
44+
45+
## Use
46+
47+
In your Express application, import Sentry and set up the error handler.
48+
49+
<PlatformContent includePath="getting-started-use" />
50+
51+
## Run
52+
53+
Adjust the Node.js call for your application to use the [--require](https://nodejs.org/api/cli.html#-r---require-module) or [--import](https://nodejs.org/api/cli.html#--importmodule) parameter and point it at `instrument.js`
54+
55+
<PlatformContent includePath="getting-started-run" />
4256

43-
<Include name="node-esm-compatibility" />
57+
<Include name="node-esm-compatibility" />
4458

4559
<PlatformContent includePath="getting-started-sourcemaps" />
4660

docs/platforms/javascript/guides/fastify/index.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ yarn add @sentry/node
3030

3131
## Configure
3232

33-
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your application - otherwise, auto-instrumentation of these modules will _not_ work.
33+
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your applicationotherwise, auto-instrumentation of these modules will _not_ work.
3434

3535
Once this is done, Sentry's Node SDK captures unhandled exceptions as well as tracing data for your application.
3636

37+
We recommend creating an `instrument.js` file that imports and initializes Sentry.
38+
3739
<PlatformContent includePath="getting-started-config" />
3840

3941
If you set a `tracesSampleRate`, performance instrumentation will automatically be enabled for you. See <PlatformLink to="/performance/instrumentation/automatic-instrumentation">Automatic Instrumentation</PlatformLink> to learn about all the things that the SDK automatically instruments for you.
4042

41-
You can also manually capture performance data - see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
43+
You can also manually capture performance data—see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
44+
45+
## Use
46+
47+
In your Fastify application, import Sentry and set up the error handler.
48+
49+
<PlatformContent includePath="getting-started-use" />
50+
51+
## Run
52+
53+
Adjust the Node.js call for your application to use the [--require](https://nodejs.org/api/cli.html#-r---require-module) or [--import](https://nodejs.org/api/cli.html#--importmodule) parameter and point it at `instrument.js`
54+
55+
<PlatformContent includePath="getting-started-run" />
4256

43-
<Include name="node-esm-compatibility" />
57+
<Include name="node-esm-compatibility" />
4458

4559
<PlatformContent includePath="getting-started-sourcemaps" />
4660

docs/platforms/javascript/guides/koa/index.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ yarn add @sentry/node
3030

3131
## Configure
3232

33-
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your application - otherwise, auto-instrumentation of these modules will _not_ work.
33+
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your applicationotherwise, auto-instrumentation of these modules will _not_ work.
3434

3535
Once this is done, Sentry's Node SDK captures unhandled exceptions as well as tracing data for your application.
3636

37+
We recommend creating an `instrument.js` file that imports and initializes Sentry.
38+
3739
<PlatformContent includePath="getting-started-config" />
3840

3941
If you set a `tracesSampleRate`, performance instrumentation will automatically be enabled for you. See <PlatformLink to="/performance/instrumentation/automatic-instrumentation">Automatic Instrumentation</PlatformLink> to learn about all the things that the SDK automatically instruments for you.
4042

41-
You can also manually capture performance data - see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
43+
You can also manually capture performance data—see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
44+
45+
## Use
46+
47+
In your Koa application, import Sentry and set up the error handler.
48+
49+
<PlatformContent includePath="getting-started-use" />
50+
51+
## Run
52+
53+
Adjust the Node.js call for your application to use the [--require](https://nodejs.org/api/cli.html#-r---require-module) or [--import](https://nodejs.org/api/cli.html#--importmodule) parameter and point it at `instrument.js`
54+
55+
<PlatformContent includePath="getting-started-run" />
4256

43-
<Include name="node-esm-compatibility" />
57+
<Include name="node-esm-compatibility" />
4458

4559
<PlatformContent includePath="getting-started-sourcemaps" />
4660

docs/platforms/javascript/guides/node/index.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ categories:
66
- server
77
---
88

9-
109
<PlatformContent includePath="getting-started-primer" />
1110

1211
This guide explains how to setup Sentry in your Node.js application.
@@ -15,9 +14,7 @@ This guide explains how to setup Sentry in your Node.js application.
1514

1615
Don't already have an account and Sentry project established? Head over to [sentry.io](https://sentry.io/signup/), then return to this page.
1716

18-
<Note>
19-
This guide is for version 8.0.0 and up of `@sentry/node`.
20-
</Note>
17+
<Note>This guide is for version 8.0.0 and up of `@sentry/node`.</Note>
2118

2219
## Install
2320

@@ -33,17 +30,25 @@ yarn add @sentry/node
3330

3431
## Configure
3532

36-
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your application - otherwise, auto-instrumentation of these modules will _not_ work.
33+
Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you require any other modules in your applicationotherwise, auto-instrumentation of these modules will _not_ work.
3734

3835
Once this is done, Sentry's Node SDK captures unhandled exceptions as well as tracing data for your application.
3936

37+
We recommend creating a file named `instrument.js` that imports and initializes Sentry.
38+
4039
<PlatformContent includePath="getting-started-config" />
4140

4241
If you set a `tracesSampleRate`, performance instrumentation will automatically be enabled for you. See <PlatformLink to="/performance/instrumentation/automatic-instrumentation">Automatic Instrumentation</PlatformLink> to learn about all the things that the SDK automatically instruments for you.
4342

4443
You can also manually capture performance data - see <PlatformLink to="/performance/instrumentation/custom-instrumentation">Custom Instrumentation</PlatformLink> for details.
4544

46-
<Include name="node-esm-compatibility" />
45+
## Run
46+
47+
Adjust the Node.js call for your application to use the [--require](https://nodejs.org/api/cli.html#-r---require-module) or [--import](https://nodejs.org/api/cli.html#--importmodule) parameter and point it at `instrument.js`
48+
49+
<PlatformContent includePath="getting-started-run" />
50+
51+
<Include name="node-esm-compatibility" />
4752

4853
<PlatformContent includePath="getting-started-sourcemaps" />
4954

includes/node-esm-compatibility.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
Instrumentation works out of the box for CommonJS (CJS) applications based on `require()` calls. This means that as long as your application is either natively in CJS, or compiled at build time to CJS (which is the case for most TypeScript apps, for example), everything will work without any further setup.
44

5-
Due to compatibility issues, the Sentry SDK does not currently support ESM (based on `import`). We are working on a solution for this and hope to support this soon.
5+
ECMAScript Modules (ESM) are only supported for Node v18.19.0 onwards.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
```javascript {tabTitle:CommonJS}
2+
// Ensure to require this before requiring any other modules!
3+
require('./instrument.js')
4+
5+
const connect = require('connect');
6+
const Sentry = require("@sentry/node");
7+
const app = connect();
8+
9+
Sentry.setupConnectErrorHandler(app);
10+
11+
// Add your routes, etc.
12+
13+
app.listen(3030);
14+
```
15+
16+
```javascript {tabTitle:ESM}
17+
// Ensure to import this before importing any other modules!
18+
import './instrument.js'
19+
20+
import connect from 'connect';
21+
import * as Sentry from "@sentry/node";
22+
const app = connect();
23+
24+
Sentry.setupConnectErrorHandler(app);
25+
26+
// Add your routes, etc.
27+
28+
app.listen(3030);
29+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
```javascript {tabTitle:CommonJS}
2+
// Ensure to require this before requiring any other modules!
3+
require('./instrument.js')
4+
5+
const express = require("express");
6+
const Sentry = require("@sentry/node");
7+
const app = express();
8+
9+
// Add your routes, etc.
10+
11+
// Add this after all routes and other middlewares are defined
12+
Sentry.setupExpressErrorHandler(app);
13+
14+
app.listen(3000);
15+
```
16+
17+
```javascript {tabTitle:ESM}
18+
// Ensure to import this before importing any other modules!
19+
import './instrument.js'
20+
21+
import express from "express";
22+
import * as Sentry from "@sentry/node";
23+
const app = express();
24+
25+
// Add your routes, etc.
26+
27+
// Add this after all routes and other middlewares are defined
28+
Sentry.setupExpressErrorHandler(app);
29+
30+
app.listen(3000);
31+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
```javascript {tabTitle:CommonJS}
2+
// Ensure to require this before requiring any other modules!
3+
require('./instrument.js')
4+
5+
const { fastify } = require("fastify");
6+
const Sentry = require("@sentry/node");
7+
const app = fastify();
8+
9+
Sentry.setupFastifyErrorHandler(app);
10+
11+
// Add your routes, etc.
12+
13+
app.listen({ port: 3030 });
14+
```
15+
16+
```javascript {tabTitle:ESM}
17+
// Ensure to import this before importing any other modules!
18+
import './instrument.js'
19+
20+
import { fastify } from "fastify";
21+
import * as Sentry from "@sentry/node";
22+
const app = fastify();
23+
24+
Sentry.setupFastifyErrorHandler(app);
25+
26+
// Add your routes, etc.
27+
28+
app.listen({ port: 3030 });
29+
```

0 commit comments

Comments
 (0)