Next.js

Sentry's Next.js SDK enables automatic reporting of errors and performance data.

Features:

  • Automatic Error Tracking with source maps for both JavaScript and TypeScript
  • 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
  • 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

On this page, we get you up and running with Sentry's SDK.

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

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" });
};

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