1--- 2title: Using Sentry 3maxHeadingDepth: 4 4description: A guide on installing and configuring Sentry for crash reporting. 5--- 6 7import { ConfigReactNative, ConfigClassic } from '~/components/plugins/ConfigSection'; 8import PlatformsSection from '~/components/plugins/PlatformsSection'; 9import { Collapsible } from '~/ui/components/Collapsible'; 10import { Terminal } from '~/ui/components/Snippet'; 11import { Step } from '~/ui/components/Step'; 12import { Tab, Tabs } from '~/ui/components/Tabs'; 13import { CODE } from '~/ui/components/Text'; 14 15[Sentry](http://getsentry.com/) is a crash reporting platform that provides you with "real-time insight into production deployments with info to reproduce and fix crashes". 16 17It notifies you of exceptions or errors that your users run into while using your app, and organizes them for you on a web dashboard. Reported exceptions include stacktraces, device info, version, and other relevant context automatically; you can also provide additional context that is specific to your application, like the current route and user id. 18 19## Why sentry-expo? 20 21- Sentry treats React Native as a first-class citizen and we have collaborated with Sentry to make sure Expo is, too. 22- It's very easy to set up and use 23- It scales to meet the demands of even the largest projects. 24- We trust it for our projects at Expo. 25- It is free for up to 5,000 events per month. 26- It streamlines your error-reporting code across iOS, Android, and web 27 28> Native crash reporting is not available with the classic build system (`expo build:[ios|android]`), but is available via EAS Build. 29 30<PlatformsSection title="Platform compatibility" android emulator ios simulator web /> 31 32## Installing and configuring Sentry 33 34### Step 1: Sign up for a Sentry account and create a project 35 36Before getting real-time updates on errors and making your app generally incredible, you'll need to make sure you've created a Sentry project. Here's how to do that: 37 38<Step label="1"> 39 [Sign up for Sentry](https://sentry.io/signup/) (it's free), and create a project in your 40 Dashboard. Take note of your **organization name**, **project name**, and **`DSN`**; you'll need 41 them later. - **organization name** is available in your `Organization settings` tab - **project 42 name** is available in your project's `Settings` > `Projects` tab (find it in the list) - 43 **`DSN`** is available in your project's `Settings` > `Projects` > **Project name** > `Client Keys 44 (DSN)` tab 45</Step> 46 47<Step label="2"> 48 Go to the [Sentry API section](https://sentry.io/settings/account/api/auth-tokens/), and create an 49 **auth token**. The token requires the scopes: `org:read`, `project:releases`, and 50 `project:write`. Save this, too. 51</Step> 52 53Once you have each of these: organization name, project name, DSN, and auth token, you're all set! 54 55### Step 2: Installation 56 57In your project directory, run: 58 59<Terminal cmd={['$ npx expo install sentry-expo']} /> 60 61`sentry-expo` also requires some additional Expo module packages. To install them, run: 62 63<Terminal 64 cmd={[ 65 '$ npx expo install expo-application expo-constants expo-device expo-updates @sentry/react-native', 66 ]} 67/> 68 69### Step 3: Code 70 71#### Initialization 72 73Add the following to your app's main file such as **App.js**: 74 75```js 76import * as Sentry from 'sentry-expo'; 77 78Sentry.init({ 79 dsn: 'YOUR DSN HERE', 80 enableInExpoDevelopment: true, 81 debug: true, // If `true`, Sentry will try to print out useful debugging information if something goes wrong with sending the event. Set it to `false` in production 82}); 83``` 84 85#### Usage 86 87Depending on which platform you are on (mobile or web), use the following methods to access any `@sentry/*` methods for instrumentation, performance, capturing exceptions and so on: 88 89- For React Native, access any `@sentry/react-native` exports with `Sentry.Native.*` 90- For web, access any `@sentry/browser` exports with `Sentry.Browser.*` 91 92```js 93// Access any @sentry/react-native exports via: 94// Sentry.Native.* 95 96// Access any @sentry/browser exports via: 97// Sentry.Browser.* 98 99// The following example uses `captureException()` from Sentry.Native.* to capture errors: 100try { 101 // your code 102} catch (error) { 103 Sentry.Native.captureException(error); 104} 105``` 106 107### Step 4: App Configuration 108 109Configuring sentry-expo is done through the config plugin in your **app.json** or **app.config.js**. 110 111<ConfigClassic> 112 113The instructions below will still apply, with the exception of the `plugin` configuration. Config plugins have no effect when using the classic build system. 114 115</ConfigClassic> 116 117<ConfigReactNative> 118 119If you use bare workflow, **you should not use the `plugins` property in app.json**. Instead, use `yarn sentry-wizard -i reactNative -p ios android` to configure your native projects. This `sentry-wizard` command will add an extra: 120 121```js 122import * as Sentry from '@sentry/react-native'; 123 124Sentry.init({ 125 dsn: 'YOUR DSN', 126}); 127``` 128 129to your root project file (usually **App.js**), so make sure you remove it (but keep the `sentry-expo` import and original `Sentry.init` call!) 130 131</ConfigReactNative> 132 133#### Configure a `postPublish` hook 134 135Add `expo.hooks` property to your project's **app.json** or **app.config.js** file: 136 137```json app.json 138{ 139 "expo": { 140 /* @hide ... your existing configuration */ /* @end */ 141 "hooks": { 142 "postPublish": [ 143 { 144 "file": "sentry-expo/upload-sourcemaps", 145 "config": { 146 "organization": "your sentry organization slug here", 147 "project": "your sentry project name here", 148 "authToken": "your auth token here" 149 } 150 } 151 ] 152 } 153 } 154} 155``` 156 157The `authToken` value can be generated from the [Sentry API page](https://sentry.io/settings/account/api/). 158 159<Collapsible summary="Prefer to use environment variables instead of storing values in app.json?"> 160 161You can also use environment variables for your config, if you prefer: 162 163- organization → `SENTRY_ORG` 164- project → `SENTRY_PROJECT` 165- authToken → `SENTRY_AUTH_TOKEN` 166 167</Collapsible> 168 169<Collapsible summary="Additional configuration options"> 170 171In addition to the required config fields above, you can also provide these **optional** fields: 172 173- `setCommits` : boolean value indicating whether or not to tell Sentry about which commits are associated with a new release. This allows Sentry to pinpoint which commits likely caused an issue. 174- `deployEnv` : string indicating the deploy environment. This will automatically send an email to Sentry users who have committed to the release that is being deployed. 175- `distribution` : The name/value to give your distribution (you can think of this as a sub-release). Expo defaults to using your `version` from app.json. **If you provide a custom `distribution`, you must pass the same value to `dist` in your call to `Sentry.init()`, otherwise you will not see stacktraces in your error reports.** 176- `release` : The name you'd like to give your release (e.g. `release-feature-ABC`). This defaults to a unique `revisionId` of your JS bundle. **If you provide a custom `release`, you must pass in the same `release` value to `Sentry.init()`, otherwise you will not see stacktraces in your error reports.** 177- `url` : your Sentry URL, only necessary when self-hosting Sentry. 178 179> You can also use environment variables for your config, if you prefer: 180> 181> - setCommits → `SENTRY_SET_COMMITS` 182> - deployEnv → `SENTRY_DEPLOY_ENV` 183> - distribution → `SENTRY_DIST` 184> - release → `SENTRY_RELEASE` 185> - url → `SENTRY_URL` 186 187</Collapsible> 188 189#### Add the Config Plugin 190 191Add `expo.plugins` to your project's **app.json** or **app.config.js** file: 192 193{/* prettier-ignore */} 194```json app.json 195{ 196 "expo": { 197 "plugins": ["sentry-expo"] 198 /* @hide ... your existing configuration */ /* @end */ 199 } 200} 201``` 202 203## Source Maps 204 205{/* TODO: Drop `expo publish` mention */} 206 207With the `postPublish` hook in place, now all you need to do is run `expo publish` and the source maps will be uploaded automatically. We automatically assign a unique release version for Sentry each time you hit publish, based on the version you specify in **app.json** and a release id on our backend -- this means that if you forget to update the version but hit publish, you will still get a unique Sentry release. 208 209> This hook can also be used as a `postExport` hook if you're [self-hosting your updates](../distribution/custom-updates-server.mdx). 210 211### Uploading source maps at build time 212 213> Note: Disregard the following if you're using the classic build system (`expo build:[android|ios]`). 214 215With `expo-updates`, release builds of both iOS and Android apps will create and embed a new update from your JavaScript source at build-time. **This new update will not be published automatically** and will exist only in the binary with which it was bundled. Since it isn't published, the source maps aren't uploaded in the usual way like they are when you run `expo publish` (actually, we are relying on Sentry's native scripts to handle that). Because of this you have some extra things to be aware of: 216 217- Your `release` will automatically be set to Sentry's expected value- `${bundleIdentifier}@${version}+${buildNumber}` (iOS) or `${androidPackage}@${version}+${versionCode}` (Android). 218- Your `dist` will automatically be set to Sentry's expected value: `${buildNumber}` (iOS) or `${versionCode}` (Android). 219- The configuration for build time source maps comes from the **android/sentry.properties** and **ios/sentry.properties** files. For more information, see [Sentry's documentation](https://docs.sentry.io/clients/java/config/#configuration-via-properties-file). Manual configuration is only required for bare projects, the [sentry-expo config plugin handles it otherwise](#add-the-config-plugin). 220- Configuration for `expo publish` and `npx expo export` for projects is done via **app.json**, whether using bare workflow or not. 221 222Skipping or misconfiguring either of these can lead to invalid source maps, and you won't see human-readable stacktraces in your errors. 223 224### Uploading source maps for updates 225 226> This requires SDK 47, with `[email protected]` or above and `[email protected]` or above. 227 228If you're using EAS Update, or if you're self-hosting your updates (this means you run `npx expo export` manually), you need to take the following steps to upload the source maps for your update to Sentry: 229 230- Run `eas update`. This will generate a **dist** folder in your project root, which contains your JavaScript bundles and source maps. This command will also output the 'Android update ID' and 'iOS update ID' that we'll need in the next step. 231- Copy or rename the bundle names in the **dist/bundles** folder to match **index.android.bundle** (Android) or **main.jsbundle** (iOS). 232- Next, you can use the Sentry CLI to upload your bundles and source maps: 233 - `release name` should be set to `${bundleIdentifier}@${version}+${buildNumber}` (iOS) or `${androidPackage}@${version}+${versionCode}` (Android), so for example `[email protected]+1`. 234 - `dist` should be set to the Update ID that `eas update` generated. 235 236<Tabs> 237 <Tab label="Android"> 238 <Terminal 239 cmd={[ 240 '$ node_modules/@sentry/cli/bin/sentry-cli releases \\', 241 ' files <release name> \\', 242 ' upload-sourcemaps \\', 243 ' --dist <Android Update ID> \\', 244 ' --rewrite \\', 245 ' dist/bundles/index.android.bundle dist/bundles/android-<hash>.map', 246 ]} 247 /> 248 </Tab> 249 <Tab label="iOS"> 250 <Terminal 251 cmd={[ 252 '$ node_modules/@sentry/cli/bin/sentry-cli releases \\', 253 ' files <release name> \\', 254 ' upload-sourcemaps \\', 255 ' --dist <iOS Update ID> \\', 256 ' --rewrite \\', 257 ' dist/bundles/main.jsbundle dist/bundles/ios-<hash>.map', 258 ]} 259 /> 260 </Tab> 261</Tabs> 262 263For more information, see Sentry's [instructions for uploading the bundle and source maps](https://docs.sentry.io/platforms/react-native/sourcemaps/#3-upload-the-bundle-and-source-maps). 264 265The steps above define a new 'dist' on Sentry, under the same release as your full app build, and associate the source maps with this new dist. If you want to customize this behavior, you can pass in your own values for `release` and `dist` when you initialize Sentry in your code. For example: 266 267```js 268import * as Sentry from 'sentry-expo'; 269import * as Updates from 'expo-updates'; 270 271Sentry.init({ 272 dsn: 'YOUR DSN', 273 release: 'my release name', 274 dist: 'my dist', 275}); 276``` 277 278These values should match the values you pass to the `sentry-cli` when uploading your source maps. 279 280### Testing Sentry 281 282When building tests for your application, you want to assert that the right flow-tracking or error is being sent to Sentry, but without really sending it to Sentry servers. This way you won't swamp Sentry with false reports during test running and other CI operations. 283 284[`sentry-testkit`](https://wix.github.io/sentry-testkit) enables Sentry to work natively in your application, and by overriding the default Sentry transport mechanism, the report is not really sent but rather logged locally into memory. In this way, the logged reports can be fetched later for your own usage, verification, or any other use you may have in your local developing/testing environment. 285 286For more information on how to get started, see [`sentry-testkit` documentation](https://wix.github.io/sentry-testkit/). 287 288> If you're using `jest`, make sure to add `@sentry/.*` and `sentry-expo` to your `transformIgnorePatterns`. 289 290## Error reporting semantics 291 292In order to ensure that errors are reported reliably, Sentry defers reporting the data to their backend until the next time you load the app after a fatal error rather than trying to report it upon catching the exception. It saves the stacktrace and other metadata to `AsyncStorage` and sends it immediately when the app starts. 293 294## Disabled by default in dev 295 296Unless `enableInExpoDevelopment: true` is set, all your dev/local errors will be ignored and only app releases will report errors to Sentry. You can call methods like `Sentry.Native.captureException(new Error('Oops!'))` but these methods will be no-op. 297 298## Troubleshooting 299 300<Collapsible summary={<>I'm seeing <CODE>error: project not found</CODE> in my build logs</>}> 301 302This error is caused by the script that tries to upload source maps during build. 303 304Make sure your `organization`, `project` and `authToken` properties are set correctly. 305 306</Collapsible> 307 308<Collapsible summary={<>`expo-dev-client` transactions never finish</>}> 309 310This error is caused by the HTTP request tracking, which creates spans for log requests to the development server. To fix this, stop creating spans by adding the following code snippet: 311 312```js 313import * as Sentry from 'sentry-expo'; 314import * as Network from 'expo-network'; 315 316const devServerPort = 8081; 317let devServerIpAddress: string | null = null; 318Network.getIpAddressAsync().then((ip) => {devServerIpAddress = ip}); 319 320Sentry.init({ 321 tracesSampleRate: 1.0, 322 integrations: [ 323 new Sentry.Native.ReactNativeTracing({ 324 shouldCreateSpanForRequest: (url) => { 325 return !__DEV__ || !url.startsWith(`http://${devServerIpAddress}:${devServerPort}/logs`); 326 }, 327 }), 328 ], 329}); 330``` 331 332</Collapsible> 333 334 335## Learn more about Sentry 336 337Sentry does more than just catch fatal errors, learn more about how to use Sentry from their [JavaScript usage docs](https://docs.sentry.io/platforms/javascript/). 338