Skip to content

Commit

Permalink
docs(node): Update Node README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jul 15, 2024
1 parent 15fdf09 commit a6bcebb
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions packages/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ yarn add @sentry/node

## Usage

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.

You need to create a file named `instrument.js` that imports and initializes Sentry:

```js
// CJS Syntax
const Sentry = require('@sentry/node');
Expand All @@ -32,27 +37,39 @@ Sentry.init({
// ...
});
```
You need to require or import the `instrument.js` file before importing any other modules in your application. This is
necessary to ensure that Sentry can automatically instrument all modules in your application:

Note that it is necessary to initialize Sentry **before you import any package that may be instrumented by us**.
```js
// Import this first!
import "./instrument";

[More information on how to set up Sentry for Node in v8.](https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-node.md)
// Now import other modules
import http from "http";

// Your application code goes here
```

### ESM Support

Due to the way OpenTelemetry handles instrumentation, this only works out of the box for CommonJS (`require`)
applications.
When running your application in ESM mode, you should use the Node.js
[`--import`](https://nodejs.org/api/cli.html#--importmodule) command line option to ensure that Sentry is loaded before
the application code is evaluated.

There is experimental support for running OpenTelemetry with ESM (`"type": "module"`):
Adjust the Node.js call for your application to use the `--import` parameter and point it at `instrument.js`, which
contains your `Sentry.init`() code:

```bash
node --experimental-loader=@opentelemetry/instrumentation/hook.mjs ./app.js
# Note: This is only available for Node v18.19.0 onwards.
node --import ./instrument.mjs app.mjs
```

You'll need to install `@opentelemetry/instrumentation` in your app to ensure this works.
If it is not possible for you to pass the `--import` flag to the Node.js binary, you can alternatively use the
`NODE_OPTIONS` environment variable as follows:

See
[OpenTelemetry Instrumentation Docs](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation#instrumentation-for-es-modules-in-nodejs-experimental)
for details on this - but note that this is a) experimental, and b) does not work with all integrations.
```bash
NODE_OPTIONS="--import ./instrument.mjs" npm run start
```

## Links

Expand Down

0 comments on commit a6bcebb

Please sign in to comment.