Setting Up Sentry for Frontend
What you'll learn: How to install and configure Sentry to automatically capture JavaScript errors and unhandled promise rejections in your frontend application.
Why Sentry After Error Tracking Basics
In the previous lesson, you learned why client-side error tracking matters. Now it's time to implement the industry-standard solution: Sentry. Think of Sentry as an always-watching safety net that catches every error your users encounter—even the ones you'd never discover through manual testing.
Installing Sentry
First, install the Sentry browser SDK:
npm install @sentry/browser
Basic Configuration
Initialize Sentry as early as possible in your application lifecycle—ideally in your main entry file before any other code runs:
import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: "https://your-dsn@sentry.io/project-id",
environment: "production",
tracesSampleRate: 0.1, // Sample 10% of transactions
});
The DSN (Data Source Name) is your unique project identifier from Sentry's dashboard. It tells the SDK where to send error reports.
What Gets Captured Automatically
Once initialized, Sentry automatically captures:
- Uncaught exceptions (syntax errors, reference errors, etc.)
- Unhandled promise rejections (forgotten
.catch()blocks) - Console errors (optional, configurable)
No additional code needed—just initialize and you're protected. The SDK wraps native error handlers behind the scenes.
Environment Separation
Notice the environment option? This prevents development errors from polluting production reports. Only initialize Sentry when appropriate:
if (window.location.hostname !== "localhost") {
Sentry.init({
// ... config
});
}
Key Takeaway: Installing Sentry requires just one npm install and a single init() call—after that, all JavaScript errors and unhandled promise rejections are automatically captured and reported to your dashboard without any additional tracking code.