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.

We've released v8 of the JavaScript SDKs. If you're using version 7.x, we recommend upgrading to the latest version. Check out the Migration docs to learn how to make the switch.

Don't have a Sentry account? Sign up for Sentry for free, 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").