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 Performance Monitoring 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

next dev --turbo is not supported while Turbopack is in beta. Running your devserver with Turbopack will cause the Sentry Next.js SDK not to be loaded in the browser. Data from serverside instrumentation will also be inaccurate or incomplete.

If you're seeing deprecation warnings in your code, please note that we're currently working on version 8 of the JavaScript SDKs. In v8, some methods and properties will be removed or renamed. Check out the Migration docs and learn how to update your code to be compatible with v8.

Don't already have an account and Sentry project established? Head over to sentry.io, then return to this page.

If you prefer to follow video instructions, see How to Install the Sentry Next.js SDK in 60 Seconds.

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