# CommonJS (CJS) | Sentry for Hapi

Are you unsure if you should use this installation method? Review our [installation methods](https://docs.sentry.io/platforms/javascript/guides/hapi/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");
//  profiling
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
//  profiling

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

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

  integrations: [
    // Add our Profiling integration
    nodeProfilingIntegration(),
  ],
  //  profiling
  //  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,
  //  performance
  //  profiling

  // Set profilesSampleRate to 1.0 to profile 100%
  // of sampled transactions.
  // This is relative to tracesSampleRate
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/guides/node/configuration/options/#profilesSampleRate
  profilesSampleRate: 1.0,
  //  profiling
  //  logs

  // Enable logs to be sent to Sentry
  _experiments: { enableLogs: true },
  //  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 Sentry = require("@sentry/node");
const Hapi = require("@hapi/hapi");

const init = async () => {
  const server = Hapi.server({
    port: 3030,
    host: "localhost",
  });

  // All your routes etc.

  await Sentry.setupHapiErrorHandler(server);

  await server.start();
  console.log("Server running on %s", server.info.uri);
};

init();
```
