Next.js

Next.js is a flexible React framework that gives you building blocks to create fast, full-stack web applications. Learn how to set it up with Sentry.

Features:

  • Automatic Error Tracking with source maps for both JavaScript and TypeScript
  • Automatic Tracing for both the client and server
  • Errors and Performance support for Middleware and Edge routes in Vercel's edge runtime. Due to complexities with the runtime, some features of errors like stack traces may not be as expected. Requires sentry.edge.config.js, more info here
  • Events enriched with device data
  • Breadcrumbs created for outgoing HTTP request with XHR and Fetch, and console logs
  • Release health for tracking crash-free users and sessions

The Sentry SDK does not fully support next dev --turbo while Turbopack does not ship all the features required to run Sentry.

Currently, when you run your devserver with Turbopack, the Sentry Next.js SDK will not be loaded in the browser. Keep this in mind when configuring the SDK.

Note that this only affects the devserver. The Sentry SDK will run without issues when you do a production build, even if you develop locally with Turbopack.

To support the SDK in the browser with Turbopack, follow the feature request for client-side instrumentation in the Next.js repository.

You can track Sentry's Turbopack support in the GitHub Issue in the Sentry SDK repository.

Sentry captures data by using an SDK within your application’s runtime.

We recommend installing the SDK through our installation wizard:

Copied
npx @sentry/wizard@latest -i nextjs

The wizard will prompt you to log in to Sentry. It will then automatically do the following steps for you:

  • create config files with the default Sentry.init() calls for each runtime (node, browser, edge)
  • add a Next.js instrumentation hook to your project (instrumentation.ts)
  • create or update your Next.js config with the default Sentry configuration
  • create .sentryclirc with an auth token to upload source maps (this file is automatically added to .gitignore)
  • add an example page to your app to verify your Sentry setup

After the wizard setup is completed, the SDK will automatically capture unhandled errors, and monitor performance. You can also manually capture errors.

To learn how to connect your app to Sentry and deploy it on Vercel, see the Vercel integration.

If the setup through the wizard doesn't work for you, you can also set up the SDK manually.

To complete your configuration, add options to your Sentry.init() calls. Here you can also set context data - data about the user, for example, or tags, or even arbitrary data - which will be added to every event sent to Sentry.

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

Add a button to a frontend component that throws an error:

pages/index.js
Copied
<button
  type="button"
  onClick={() => {
    throw new Error("Sentry Frontend Error");
  }}
>
  Throw error
</button>;

And throw an error in an API route:

pages/api/error.js
Copied
export default (req, res) => {
  throw new Error("API throw error test");
  res.status(200).json({ name: "John Doe" });
};

Errors triggered from within Browser DevTools are sandboxed, so they will not trigger error monitoring. Keep this in mind when verifying your Sentry SDK installation.

Learn more about manually capturing an error or message in our Usage documentation.

To view and resolve the recorded error, log into sentry.io and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

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").