CommonJS (CJS)
Learn about running Sentry in an CJS application.
Are you unsure if you should use this installation method? Review our installation methods.
Most node applications today are either written in CommonJS (CJS), or compiled to CJS before running them. CommonJS 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.
You need to create a file named instrument.js
that imports and initializes Sentry:
instrument.js
Copied
const Sentry = require("@sentry/nestjs");
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
// Ensure to call this before requiring any other modules!
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
],
// Add Tracing by setting tracesSampleRate
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
// Set sampling rate for profiling
// This is relative to tracesSampleRate
profilesSampleRate: 1.0,
});
You need to require or import the instrument.js
file before requiring any other modules in your application. This is necessary to ensure that Sentry can automatically instrument all modules in your application:
main.ts
Copied
// Import this first!
import "./instrument";
// Now import other modules
import * as Sentry from "@sentry/nestjs";
import {
BaseExceptionFilter,
HttpAdapterHost,
NestFactory,
} from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const { httpAdapter } = app.get(HttpAdapterHost);
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
await app.listen(3000);
}
bootstrap();
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").
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").