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:
Now bind an onError
hook to report unhandled exceptions to Sentry:
Copied
const app = new Hono()
// Add an onError hook to report unhandled exceptions to Sentry.
.onError((err, c) => {
// Report _all_ unhandled errors.
Sentry.captureException(err);
if (err instanceof HTTPException) {
return err.getResponse();
}
// Or just report errors which are not instances of HTTPException
// Sentry.captureException(err);
return c.json({ error: "Internal server error" }, 500);
})
// Bind global context via Hono middleware
.use((c, next) => {
Sentry.setUser({
email: c.session.user.email,
});
Sentry.setTag("project_id", c.session.projectId);
return next();
})
// Your routes...
.get("/", () => {
// ...
});
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
import * as Sentry from "@sentry/node";
import { Hono, HTTPException } from "hono";
const app = new Hono()
// Add an onError hook to report unhandled exceptions to Sentry.
.onError((err, c) => {
// Report _all_ unhandled errors.
Sentry.captureException(err);
if (err instanceof HTTPException) {
return err.getResponse();
}
// Or just report errors which are not instances of HTTPException
// Sentry.captureException(err);
return c.json({ error: "Internal server error" }, 500);
});
// Your routes...
app.get("/", () => {
// ...
});
export default app;
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").