Next.js

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

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

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.

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)

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 in your frontend. You can also manually capture errors.

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

Copied
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
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/hello.js
Copied
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
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.

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