Skip to content

docs(js): Update Nest.js installation method content #13765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 28, 2025
1 change: 0 additions & 1 deletion docs/platforms/javascript/common/install/commonjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ supported:
- javascript.hapi
- javascript.hono
- javascript.koa
- javascript.nestjs
---

<Alert>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ supported:
- javascript.hapi
- javascript.hono
- javascript.koa
- javascript.nestjs
---

<Alert>
Expand Down
1 change: 0 additions & 1 deletion docs/platforms/javascript/common/install/esm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ supported:
- javascript.hapi
- javascript.hono
- javascript.koa
- javascript.nestjs
---

<Alert>
Expand Down
1 change: 0 additions & 1 deletion docs/platforms/javascript/common/install/esm__v8.x.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ supported:
- javascript.hapi
- javascript.hono
- javascript.koa
- javascript.nestjs
noindex: true
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ supported:
- javascript.hapi
- javascript.hono
- javascript.koa
- javascript.nestjs
---

<Alert>
Expand Down
17 changes: 17 additions & 0 deletions docs/platforms/javascript/guides/nestjs/install/commonjs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: CommonJS (CJS)
sidebar_order: 9
description: "Learn about running Sentry in an CJS application."
supported:
- javascript.nestjs
---

<Alert>
Are you unsure if you should use this installation method? Review our
[installation methods](../).
</Alert>

Most node applications today are either written in or compiled to CommonJS (CJS), which uses `require()` to load modules.
Nest.js, by default, transpiles your TypeScript code into CommonJS.

You can follow the installation instructions in the SDK's <PlatformLink to="/">Quick Start guide</PlatformLink>.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: ESM Without CLI Flag
sidebar_order: 11
description: "Learn about running Sentry in an ESM application, without the --import flag."
supported:
- javascript.nestjs
---

<Alert>
Are you unsure if you should use this installation method? Review our
[installation methods](../).
</Alert>

When running your application in [ECMAScript Modules](https://nodejs.org/api/esm.html#introduction) (ESM) mode, you'll 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.

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

This installation method has the fundamental restriction that only native Node.js APIs can be instrumented (such as `fetch` and the `http` module).

As a result, the Sentry SDK will **not** capture data from database calls, queues, ORMs, third-party libraries, or other framework-specific data.

We recommend using this only if the `--import` flag is not an option for you.

</Alert>

**Step 1:** Create a file named `instrument.mjs` that imports and initializes Sentry:

```javascript {tabTitle:ESM} {filename: instrument.mjs}
import * as Sentry from "@sentry/nestjs";

// Ensure to call this before importing any other modules!
Sentry.init({
dsn: "___PUBLIC_DSN___",

// Add Tracing by setting tracesSampleRate
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
```

**Step 2:** Import the `instrument.mjs` file before importing any other modules in your in the `main.ts` file of your application. This ensures that Sentry can automatically instrument all modules in your application:

```javascript {filename: main.ts}
// Import this first!
import "./instrument";

// Now import other modules
import { NestFactory } from "@nestjs/core";
// ...
// Your application code goes here
```
70 changes: 70 additions & 0 deletions docs/platforms/javascript/guides/nestjs/install/esm.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: ESM (MJS)
sidebar_order: 10
description: "Learn about running Sentry in an ESM application."
supported:
- javascript.nestjs
---

<Alert>
Are you unsure if you should use this installation method? Review our
[installation methods](../).
</Alert>

When running your application in [ECMAScript Modules](https://nodejs.org/api/esm.html#introduction) (ESM) mode, you can't use `require()` to load modules. Instead, you have to use the `--import` command line options to load a module before the application starts.

**Step 1:** Create a file named `instrument.mjs` that imports and initializes Sentry:

```javascript {tabTitle:ESM} {filename: instrument.mjs}
import * as Sentry from "@sentry/nestjs";

// Ensure to call this before importing any other modules!
Sentry.init({
dsn: "___PUBLIC_DSN___",

// Add Tracing by setting tracesSampleRate
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
```

**Step 2:** Adjust your application's start command to use the [--import](https://nodejs.org/api/cli.html#--importmodule) parameter:

```bash
# Note: This is only available for Node v18.19.0 onwards.
"start": "--import ./instrument.mjs nest start"
```

If you can't pass the `--import` flag to the Node.js binary, you can alternatively use the `NODE_OPTIONS` environment variable as follows:

```bash
NODE_OPTIONS="--import ./instrument.mjs" npm run start
```

<Alert>We do not support ESM in Node versions before 18.19.0.</Alert>

## Troubleshooting ESM Instrumentation

By default, all packages are automatically wrapped by
[import-in-the-middle](https://www.npmjs.com/package/import-in-the-middle) to
aid instrumenting them.

If `import-in-the-middle` encounters problems wrapping a package, you may see
syntax errors at runtime or logged errors in your console:

```logs
SyntaxError: The requested module '...' does not provide an export named '...'
(node:3368) Error: 'import-in-the-middle' failed to wrap 'file://../../path/to/file.js'
```

To confirm that these errors are caused by `import-in-the-middle`,
disable it by setting `registerEsmLoaderHooks` to false. Note, this will also
disable tracing instrumentation:

```javascript {tabTitle:ESM} {filename: instrument.mjs} {4}
import * as Sentry from "@sentry/nestjs";

Sentry.init({
registerEsmLoaderHooks: false,
});
```
85 changes: 85 additions & 0 deletions docs/platforms/javascript/guides/nestjs/install/esm__v8.x.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
title: ESM (MJS)
sidebar_order: 10
description: "Learn about running Sentry in an ESM application."
supported:
- javascript.nestjs
noindex: true
---

<Alert>
Are you unsure if you should use this installation method? Review our
[installation methods](../).
</Alert>

When running your application in [ECMAScript Modules](https://nodejs.org/api/esm.html#introduction) (ESM) mode, you can't use `require()` to load modules. Instead, you have to use the `--import` command line options to load a module before the application starts.

**Step 1:** Create a file named `instrument.mjs` that imports and initializes Sentry:

```javascript {tabTitle:ESM} {filename: instrument.mjs}
import * as Sentry from "@sentry/nestjs";

// Ensure to call this before importing any other modules!
Sentry.init({
dsn: "___PUBLIC_DSN___",

// Add Tracing by setting tracesSampleRate
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
```

**Step 2:** Adjust your application's start command to use the [--import](https://nodejs.org/api/cli.html#--importmodule) parameter:

```bash
# Note: This is only available for Node v18.19.0 onwards.
"start": "--import ./instrument.mjs nest start"
```

If you can't pass the `--import` flag to the Node.js binary, you can alternatively use the `NODE_OPTIONS` environment variable as follows:

```bash
NODE_OPTIONS="--import ./instrument.mjs" npm run start
```

<Alert>We do not support ESM in Node versions before 18.19.0.</Alert>

## Troubleshooting instrumentation

By default, all packages are automatically wrapped by
[import-in-the-middle](https://www.npmjs.com/package/import-in-the-middle) to
aid instrumenting them.

If `import-in-the-middle` encounters problems wrapping a package, you may see
syntax errors at runtime or logged errors in your console:

```logs
SyntaxError: The requested module '...' does not provide an export named '...'
(node:3368) Error: 'import-in-the-middle' failed to wrap 'file://../../path/to/file.js'
```

To confirm that these errors are caused by `import-in-the-middle`,
disable it by setting `registerEsmLoaderHooks` to false. Note, this will also
disable tracing instrumentation:

```javascript {tabTitle:ESM} {filename: instrument.mjs} {4}
import * as Sentry from "@sentry/nestjs";

Sentry.init({
registerEsmLoaderHooks: false,
});
```

If you are starting Sentry via `--import`, you can instruct `import-in-the-middle`
to only wrap packages that Sentry specifically instruments. To do this, you can
set the `onlyIncludeInstrumentedModules` to `true`:

```javascript {tabTitle:ESM} {filename: instrument.mjs} {4-6}
import * as Sentry from "@sentry/nestjs";

Sentry.init({
registerEsmLoaderHooks: {
onlyIncludeInstrumentedModules: true,
},
});
```
30 changes: 30 additions & 0 deletions docs/platforms/javascript/guides/nestjs/install/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Installation Methods
sidebar_order: 1
description: "Review our alternate installation methods for Sentry using Nest.js."
supported:
- javascript.nestjs
---

<PageGrid />

## How To Decide Which Installation Method To Use

Most node applications today are either written in or compiled to CommonJS (CJS), which uses `require()` to load modules.
Our recommended installation method when using CommonJS is to require the `instrument.js` file at the top of your application. However, if your application is run in [ECMAScript Modules](https://nodejs.org/api/esm.html#introduction) (ESM) mode, this will not work, in which case you can follow the [ESM docs](./esm).

Note that even if your application is written in ESM (using `import`), it may still be _run_ in CJS. In this case, you should follow the [CommonJS instructions](./commonjs).

### My application uses the default Nest.js setup

Nest.js transpiles to CJS by default, so you should follow the [CommonJS installation instructions](./commonjs). Keep reading if you have a more customized application or build setup.

### My application uses `require`

If you're using `require()` in your application, you should follow the [CommonJS instructions](./commonjs).

### My application uses `import`

If you're using `import` in your application, your installation method depends on how your application is _run_. If you compile your application (for example, into a `/dist` folder or similar) before running it, you need to check how the compiled code looks like. Is the compiled code using `require`? Then you should follow the [CommonJS instructions](./commonjs). If the compiled code is using `import`, you should follow the [ESM instructions](./esm).

If you do not compile your code, you'll need to follow the [ESM instructions](./esm).