ESM Without CLI Flag

Learn about running Sentry in an ESM application, without the --import flag.

When running your application in ECMAScript Modules (ESM) mode, you'll most likely want to follow the ESM instructions. 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.

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

instrument.mjs
Copied
import * as Sentry from "@sentry/nestjs";

// Ensure to call this before importing any other modules!
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",

  // 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:

main.ts
Copied
// Import this first!
import "./instrument";

// Now import other modules
import { NestFactory } from "@nestjs/core";
// ...
// Your application code goes here
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").