Next.js

Currently, the minimum Next.js supported version is 10.0.8.

Features:

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. However, using a custom server is not currently 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.

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 token 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.

To capture Next.js API Route errors and monitor server performance, you need to wrap your handlers with a Sentry function:

pages/api/*
Copied
import { withSentry } from "@sentry/nextjs";

const handler = async (req, res) => {
  res.status(200).json({ name: "John Doe" });
};

export default withSentry(handler);

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
import { withSentry } from "@sentry/nextjs";

const handler = async (req, res) => {
  throw new Error("API throw error test");
  res.status(200).json({ name: "John Doe" });
};

export default withSentry(handler);

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.