1--- 2title: Using Sentry 3maxHeadingDepth: 4 4description: A guide on installing and configuring Sentry for crash reporting. 5--- 6 7import { ConfigReactNative } 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 in Expo Go, it is only available in standalone builds or development builds. 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<ConfigReactNative> 112 113If 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: 114 115```js 116import * as Sentry from '@sentry/react-native'; 117 118Sentry.init({ 119 dsn: 'YOUR DSN', 120}); 121``` 122 123to your root project file (usually **App.js**), so make sure you remove it (but keep the `sentry-expo` import and original `Sentry.init` call!) 124 125</ConfigReactNative> 126 127#### Configure a `postPublish` hook 128 129Add `expo.hooks` property to your project's **app.json** or **app.config.js** file: 130 131```json app.json 132{ 133 "expo": { 134 /* @hide ... your existing configuration */ /* @end */ 135 "hooks": { 136 "postPublish": [ 137 { 138 "file": "sentry-expo/upload-sourcemaps", 139 "config": { 140 "organization": "your sentry organization slug here", 141 "project": "your sentry project name here", 142 "authToken": "your auth token here" 143 } 144 } 145 ] 146 } 147 } 148} 149``` 150 151The `authToken` value can be generated from the [Sentry API page](https://sentry.io/settings/account/api/). 152 153<Collapsible summary="Prefer to use environment variables instead of storing values in app.json?"> 154 155You can also use environment variables for your config, if you prefer: 156 157- organization → `SENTRY_ORG` 158- project → `SENTRY_PROJECT` 159- authToken → `SENTRY_AUTH_TOKEN` 160 161</Collapsible> 162 163<Collapsible summary="Additional configuration options"> 164 165In addition to the required config fields above, you can also provide these **optional** fields: 166 167- `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. 168- `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. 169- `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.** 170- `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.** 171- `url` : your Sentry URL, only necessary when self-hosting Sentry. 172 173> You can also use environment variables for your config, if you prefer: 174> 175> - setCommits → `SENTRY_SET_COMMITS` 176> - deployEnv → `SENTRY_DEPLOY_ENV` 177> - distribution → `SENTRY_DIST` 178> - release → `SENTRY_RELEASE` 179> - url → `SENTRY_URL` 180 181</Collapsible> 182 183#### Add the Config Plugin 184 185Add `expo.plugins` to your project's **app.json** or **app.config.js** file: 186 187{/* prettier-ignore */} 188```json app.json 189{ 190 "expo": { 191 "plugins": ["sentry-expo"] 192 /* @hide ... your existing configuration */ /* @end */ 193 } 194} 195``` 196 197## Source Maps 198 199{/* TODO: Drop `expo publish` mention */} 200 201With 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. 202 203> This hook can also be used as a `postExport` hook if you're [self-hosting your updates](../distribution/custom-updates-server.mdx). 204 205### Uploading source maps at build time 206 207With `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: 208 209- Your `release` will automatically be set to Sentry's expected value- `${bundleIdentifier}@${version}+${buildNumber}` (iOS) or `${androidPackage}@${version}+${versionCode}` (Android). 210- Your `dist` will automatically be set to Sentry's expected value: `${buildNumber}` (iOS) or `${versionCode}` (Android). 211- 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). 212- Configuration for `expo publish` and `npx expo export` for projects is done via **app.json**, whether using bare workflow or not. 213 214Skipping or misconfiguring either of these can lead to invalid source maps, and you won't see human-readable stacktraces in your errors. 215 216### Uploading source maps for updates 217 218> This requires SDK 47, with `[email protected]` or above and `[email protected]` or above. 219 220If 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: 221 222- 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. 223- Copy or rename the bundle names in the **dist/bundles** folder to match **index.android.bundle** (Android) or **main.jsbundle** (iOS). 224- Next, you can use the Sentry CLI to upload your bundles and source maps: 225 - `release name` should be set to `${bundleIdentifier}@${version}+${buildNumber}` (iOS) or `${androidPackage}@${version}+${versionCode}` (Android), so for example `[email protected]+1`. 226 - `dist` should be set to the Update ID that `eas update` generated. 227 228<Tabs> 229 <Tab label="Android"> 230 <Terminal 231 cmd={[ 232 '$ node_modules/@sentry/cli/bin/sentry-cli releases \\', 233 ' files <release name> \\', 234 ' upload-sourcemaps \\', 235 ' --dist <Android Update ID> \\', 236 ' --rewrite \\', 237 ' dist/bundles/index.android.bundle dist/bundles/android-<hash>.map', 238 ]} 239 /> 240 </Tab> 241 <Tab label="iOS"> 242 <Terminal 243 cmd={[ 244 '$ node_modules/@sentry/cli/bin/sentry-cli releases \\', 245 ' files <release name> \\', 246 ' upload-sourcemaps \\', 247 ' --dist <iOS Update ID> \\', 248 ' --rewrite \\', 249 ' dist/bundles/main.jsbundle dist/bundles/ios-<hash>.map', 250 ]} 251 /> 252 </Tab> 253</Tabs> 254 255For 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). 256 257The 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: 258 259```js 260import * as Sentry from 'sentry-expo'; 261import * as Updates from 'expo-updates'; 262 263Sentry.init({ 264 dsn: 'YOUR DSN', 265 release: 'my release name', 266 dist: 'my dist', 267}); 268``` 269 270These values should match the values you pass to the `sentry-cli` when uploading your source maps. 271 272### Testing Sentry 273 274When 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. 275 276[`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. 277 278For more information on how to get started, see [`sentry-testkit` documentation](https://wix.github.io/sentry-testkit/). 279 280> If you're using `jest`, make sure to add `@sentry/.*` and `sentry-expo` to your `transformIgnorePatterns`. 281 282## Error reporting semantics 283 284In 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. 285 286## Disabled by default in dev 287 288Unless `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. 289 290## Troubleshooting 291 292<Collapsible summary={<>I'm seeing <CODE>error: project not found</CODE> in my build logs</>}> 293 294This error is caused by the script that tries to upload source maps during build. 295 296Make sure your `organization`, `project` and `authToken` properties are set correctly. 297 298</Collapsible> 299 300<Collapsible summary={<>`expo-dev-client` transactions never finish</>}> 301 302This 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: 303 304```js 305import * as Sentry from 'sentry-expo'; 306import * as Network from 'expo-network'; 307 308const devServerPort = 8081; 309let devServerIpAddress: string | null = null; 310Network.getIpAddressAsync().then(ip => { 311 devServerIpAddress = ip; 312}); 313 314Sentry.init({ 315 tracesSampleRate: 1.0, 316 integrations: [ 317 new Sentry.Native.ReactNativeTracing({ 318 shouldCreateSpanForRequest: url => { 319 return !__DEV__ || !url.startsWith(`http://${devServerIpAddress}:${devServerPort}/logs`); 320 }, 321 }), 322 ], 323}); 324``` 325 326</Collapsible> 327 328## Learn more about Sentry 329 330Sentry 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/). 331