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.jsCopied
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
// 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
// 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,
// ___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:
Copied
// 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();
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").
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").