Learn how to set up and configure Sentry in your SvelteKit application using the installation wizard, capture your first errors, and view them in Sentry.
SvelteKit version 2.0.0+ (we recommend 2.31.0 or higher for best support)
Vite version 4.2+
I'm on an older SvelteKit version than 2.31.0
Version 2.31.0 of SvelteKit introduces official support for observability and tracing. This means that Sentry can now follow the official best practice for how the SDK should be set up:
The Sentry SDK will be initialized at the correct, earliest possible time on the server, allowing for all its auto-instrumentation to work correctly. This means, you will now get spans from auto instrumentation (e.g. database queries) automatically.
The Sentry SDK picks up spans emitted from SvelteKit directly. You'll get more accurate insights into the performance of your handlers, server actions, load, and remote functions.
SvelteKit observability is still an experimental feature in SvelteKit 2.x, but we recommend giving it a try. The Sentry wizard, as well as this guide, will use it as the default way of setting up the SDK.
If you've already set up the SDK and don't want to migrate to the new setup, or don't want to upgrade to SvelteKit 2.31.0 or higher, that's fine too. Just follow this guide instead. However, note that auto instrumentation (e.g. for database queries) will not work.
Notes on SvelteKit adapter compatibility
The SvelteKit Sentry SDK is designed to work out of the box with several SvelteKit adapters and their underlying server runtimes. Here's an overview of the current support:
Fully supported Node.js runtimes
Adapter-auto for Vercel; other Node.js-based platforms might work, but we don't guarantee compatibility at this time
Adapter-vercel when used with Vercel's Node.js Lambda runtime
Non-Node.js server runtimes, such as Vercel's edge runtime, are not yet supported.
Other adapters
Other SvelteKit adapters might work, but they're not currently officially supported. We're looking into extending first-class support to more adapters in the future.
To install Sentry using the installation wizard, run the following command within your project:
Copied
npx @sentry/wizard@latest -i sveltekit
The wizard then guides you through the setup process, asking you to enable additional (optional) Sentry features for your application beyond error monitoring.
Want to learn more about these features?
Issues (always enabled): Sentry's core error monitoring product that automatically reports errors, uncaught exceptions, and unhandled rejections. If you have something that looks like an exception, Sentry can capture it.
Tracing: Track software performance while seeing the impact of errors across multiple systems. For example, distributed tracing allows you to follow a request from the frontend to the backend and back.
Session Replay: Get to the root cause of an issue faster by viewing a video-like reproduction of what was happening in the user's browser before, during, and after the problem.
Logs: Centralize and analyze your application logs to correlate them with errors and performance issues. Search, filter, and visualize log data to understand what's happening in your applications.
This guide assumes that you enable all features and allow the wizard to create an example page. You can add or remove features at any time, but setting them up now will save you the effort of configuring them manually later.
What does the installation wizard change inside your application?
Creates or updates hooks.(client|server).js to initialize the SDK and instrument SvelteKit's hooks
Creates or updates vite.config.js to add source maps upload and auto-instrumentation via Vite plugins
If you're on SvelteKit 2.31.0 or higher:
Creates instrumentation.server.js to initialize the server-side SDK
Enables server-side instrumentation and tracing in svelte.config.js
Creates .env.sentry-build-plugin with an auth token to upload source maps (this file is automatically added to .gitignore)
Adds an example page to your application to help verify your Sentry setup
You can prevent ad blockers from blocking Sentry events using tunneling. Use the tunnel option to add an API endpoint in your application that forwards Sentry events to Sentry servers.
To enable tunneling, update Sentry.init in your hooks.client.(js|ts) file with the following option:
hooks.client.(js|ts)
Copied
Sentry.init({dsn:"___PUBLIC_DSN___",
tunnel:"/tunnel",
});
This will send all events to the tunnel endpoint. However, the events need to be parsed and redirected to Sentry, so you'll need to do additional configuration on the server. You can find a detailed explanation on how to do this on our Troubleshooting page.
If you haven't tested your Sentry configuration yet, let's do it now. You can confirm that Sentry is working properly and sending data to your Sentry project by using the example page and route created by the installation wizard:
Open the example page /sentry-example-page in your browser
Click the "Throw Sample Error" button. This triggers two errors:
a frontend error
an error within the API route
Sentry captures both of these errors for you. Additionally, the button click starts a trace to measure the time it takes for the API request to complete.
Tip
Don't forget to explore the example files' code in your project to understand what's happening after your button click.
Now, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).
Important
Errors triggered from within your browser's developer tools (like the browser console) are sandboxed, so they will not trigger Sentry's error monitoring.
Need help locating the captured errors in your Sentry project?
Open the Issues page and select an error from the issues list to view the full details and context of this error. For more details, see this interactive walkthrough.
Open the Traces page and select a trace to reveal more information about each span, its duration, and any errors. For an interactive UI walkthrough, click here.
Open the Replays page and select an entry from the list to get a detailed view where you can replay the interaction and get more information to help you troubleshoot.
Open the Logs page and filter by service, environment, or search keywords to view log entries from your application. For an interactive UI walkthrough, click here.
At this point, you should have integrated Sentry into your SvelteKit application and should already be sending error and performance data to your Sentry project.
Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:
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").