Next.js

The minimum Next.js supported version is 10.0.8. While Next.js version 13 is itself supported, the new features it introduces are not yet. In particular, building with Turbopack will currently cause the Sentry SDK not to be included in your app.

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, from version 6.5.0
  • 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 stacktraces may not not be as expected. Requires sentry.edge.config.js, more info here

Under the hood the SDK relies on our React SDK on the frontend and Node SDK on the backend, which makes all features available in those SDKs also available in this SDK. Using a custom server is currently not supported.

On this page, we get you up and running with Sentry's SDK, so that it will automatically report errors and exceptions in your application.

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

Install

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

New Users

If you don't have a Sentry account yet, run this command to install Sentry on your Next.js project:

Copied
npx @sentry/wizard -s -i nextjs

Existing Users

If you have a Sentry account, run this command:

Copied
npm install --save @sentry/nextjs

Configure

Configuration should happen as early as possible in your application's lifecycle.

Though it's possible to configure everything manually, we have a wizard that will automate the initial steps. To use the wizard, run the following command from the root level of your project:

Copied
npx @sentry/wizard -i nextjs

You'll be prompted to log in to Sentry. The wizard will then automatically add these configuration files to your project:

  • create sentry.client.config.js and sentry.server.config.js with the default Sentry.init
  • create next.config.js with the default configuration
  • create sentry.properties with configuration for sentry-cli (which is used when automatically uploading source maps)
  • create .sentryclirc with the auth
    tokenIn search, a key-value pair or raw search term. Also, a value used for authorization.
    for sentry-cli (which is automatically added to the .gitignore)

See manual configuration for examples of the files created by the wizard. If any of these files already exists, it will be created with a leading underscore and you will need to merge it with the existing file manually.

To complete your configuration, add options to your two Sentry.init() calls (in sentry.client.config.js and sentry.server.config.js, respectively). In those two files 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.

Once you're set up, the SDK will automatically capture unhandled errors and promise rejections, and monitor performance in the client. You can also manually capture errors.

By default, the SDK sets the environment for events to process.env.NODE_ENV, although you can set your own.

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

Verify

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!) to suggesting an update ("yeah, this would be better").