Next.js
Sentry's Next.js SDK enables automatic reporting of errors and exceptions.
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
- Automatic Performance Monitoring for both the client and server, from version
6.5.0
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.
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:
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
andsentry.server.config.js
with the defaultSentry.init
- create
next.config.js
with the default configuration - create
sentry.properties
with configuration forsentry-cli
(which is used when automatically uploading source maps) - create
.sentryclirc
with the auth token forsentry-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/*
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
<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
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);
Errors triggered from within Browser DevTools are sandboxed, so will not trigger an error handler. Place the snippet directly in your code instead.
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.
- Package:
- npm:@sentry/nextjs
- Version:
- 8.24.0
- Repository:
- https://github.com/getsentry/sentry-javascript