---
title: "CommonJS (CJS)"
description: "Learn about running Sentry in an CJS application."
url: https://docs.sentry.io/platforms/javascript/guides/fastify/install/commonjs/
---

# CommonJS (CJS) | Sentry for Fastify

Are you unsure if you should use this installation method? Review our [installation methods](https://docs.sentry.io/platforms/javascript/guides/fastify/install.md).

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`

```javascript
const Sentry = require("@sentry/node");
// ___PRODUCT_OPTION_START___ profiling
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
// ___PRODUCT_OPTION_END___ profiling

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

  // Adds request headers and IP for users, for more info visit:
  // https://docs.sentry.io/platforms/javascript/guides/node/configuration/options/#sendDefaultPii
  sendDefaultPii: true,
  // ___PRODUCT_OPTION_START___ profiling

  integrations: [
    // Add our Profiling integration
    nodeProfilingIntegration(),
  ],
  // ___PRODUCT_OPTION_END___ profiling
  // ___PRODUCT_OPTION_START___ performance

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for tracing.
  // We recommend adjusting this value in production
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/guides/node/configuration/options/#tracesSampleRate
  tracesSampleRate: 1.0,
  // ___PRODUCT_OPTION_END___ performance
  // ___PRODUCT_OPTION_START___ profiling

  // Enable profiling for a percentage of sessions
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/configuration/options/#profileSessionSampleRate
  profileSessionSampleRate: 1.0,
  // ___PRODUCT_OPTION_END___ profiling
  // ___PRODUCT_OPTION_START___ logs

  // Enable logs to be sent to Sentry
  enableLogs: true,
  // ___PRODUCT_OPTION_END___ logs
});
```

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:

```javascript
// Require this first!
require("./instrument");

// Now require other modules
const Fastify = require("fastify");
const Sentry = require("@sentry/node");

const app = Fastify();

Sentry.setupFastifyErrorHandler(app);

// Add your routes, etc.

app.listen({ port: 3030 });
```
