Next.js
Sentry's Next.js SDK enables automatic reporting of errors and performance data.
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
- 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
The minimum Next.js supported version is 10.0.8
. While Next.js version 13 is itself supported, support for beta features like the Next.js app router are work-in-progress. In particular, building with Turbopack will currently cause the Sentry SDK not to be included in your app.
If you're using a custom server, not all SDK features may be 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.
We recommend installing the SDK through our installation wizard:
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) - create or update your Next.js config with the default Sentry configuration
- create
.sentryclirc
with an authtokenIn search, a key-value pair or raw search term. Also, a value used for authorization.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.
Configure
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.
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
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.
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").
- Package:
- npm:@sentry/nextjs
- Version:
- 7.54.0
- Repository:
- https://github.com/getsentry/sentry-javascript