|
| 1 | +--- |
| 2 | +title: Wrangler |
| 3 | +description: "Upload your Cloudflare Workers source maps using Wrangler and Sentry CLI." |
| 4 | +sidebar_order: 10 |
| 5 | +supported: |
| 6 | + - javascript.cloudflare |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +<Include name="debug-id-requirement.mdx" /> |
| 11 | + |
| 12 | +<Alert level="info"> |
| 13 | + |
| 14 | +This guide is specifically for Cloudflare Workers using Wrangler. For other build tools like Vite with Cloudflare, use the corresponding Vite guide instead. |
| 15 | + |
| 16 | +We recommend using Vite to build your worker for an easier and more reliable setup. Learn more about [Cloudflare's Vite setup](https://developers.cloudflare.com/workers/vite-plugin/get-started/). |
| 17 | + |
| 18 | +</Alert> |
| 19 | + |
| 20 | +## Automatic Setup |
| 21 | + |
| 22 | +The easiest way to configure source map uploading with Wrangler is by using the Sentry Wizard: |
| 23 | + |
| 24 | +<Include name="sourcemaps-wizard-instructions.mdx" /> |
| 25 | + |
| 26 | +If you'd rather configure source maps manually, follow the steps below. |
| 27 | + |
| 28 | +## Manual Setup |
| 29 | + |
| 30 | +### 1. Install Sentry CLI |
| 31 | + |
| 32 | +First, install Sentry CLI as a dev dependency: |
| 33 | + |
| 34 | +```bash {tabTitle:npm} |
| 35 | +npm install --save-dev @sentry/cli |
| 36 | +``` |
| 37 | + |
| 38 | +```bash {tabTitle:yarn} |
| 39 | +yarn add --dev @sentry/cli |
| 40 | +``` |
| 41 | + |
| 42 | +```bash {tabTitle:pnpm} |
| 43 | +pnpm add --save-dev @sentry/cli |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Configure Sentry CLI |
| 47 | + |
| 48 | +For more info on `sentry-cli` configuration visit the [Sentry CLI configuration docs](/cli/configuration/). |
| 49 | + |
| 50 | +Make sure `sentry-cli` is configured for your project. For that you can use environment variables: |
| 51 | + |
| 52 | +```bash {filename:.env.local} |
| 53 | +SENTRY_ORG=example-org |
| 54 | +SENTRY_PROJECT=example-project |
| 55 | +SENTRY_AUTH_TOKEN=sntrys_YOUR_TOKEN_HERE |
| 56 | +``` |
| 57 | + |
| 58 | +### 3. Modify Your Deploy Script |
| 59 | + |
| 60 | +Update your Wrangler deploy script in `package.json` to include the necessary flags for source map generation and upload: |
| 61 | + |
| 62 | +```json {filename:package.json} |
| 63 | +{ |
| 64 | + "scripts": { |
| 65 | + "deploy": "wrangler deploy --outdir dist --upload-source-maps --var SENTRY_RELEASE:$(sentry-cli releases propose-version)" |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +The key flags are: |
| 71 | + |
| 72 | +- `--outdir dist`: Specifies the output directory where source maps will be generated |
| 73 | +- `--upload-source-maps`: Forces Wrangler to generate source maps |
| 74 | +- `--var SENTRY_RELEASE:$(sentry-cli releases propose-version)`: Injects the release version as an environment variable |
| 75 | + |
| 76 | +### 4. Create a Source Map Upload Script |
| 77 | + |
| 78 | +Add a script to upload source maps to Sentry in your `package.json`: |
| 79 | + |
| 80 | +```json {filename:package.json} |
| 81 | +{ |
| 82 | + "scripts": { |
| 83 | + "sentry:sourcemaps": "_SENTRY_RELEASE=$(sentry-cli releases propose-version) && sentry-cli releases new $_SENTRY_RELEASE --org=your-org --project=your-project && sentry-cli sourcemaps upload --org=your-org --project=your-project --release=$_SENTRY_RELEASE --strip-prefix 'dist/..' dist" |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Replace `your-org` and `your-project` with your actual Sentry organization and project slugs. |
| 89 | + |
| 90 | +### 5. Add a Post-Deploy Hook |
| 91 | + |
| 92 | +Create a post-deploy script that automatically uploads source maps after deployment: |
| 93 | + |
| 94 | +```json {filename:package.json} |
| 95 | +{ |
| 96 | + "scripts": { |
| 97 | + "deploy": "wrangler deploy --outdir dist --upload-source-maps --var SENTRY_RELEASE:$(sentry-cli releases propose-version)", |
| 98 | + "postdeploy": "npm run sentry:sourcemaps" |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +This ensures that source maps are uploaded to Sentry immediately after a successful deployment. |
| 104 | + |
| 105 | +### 6. Configure Your Sentry SDK |
| 106 | + |
| 107 | +Make sure your Sentry SDK configuration includes the release information: |
| 108 | + |
| 109 | +```typescript {filename:index.ts} |
| 110 | +import * as Sentry from "@sentry/cloudflare"; |
| 111 | + |
| 112 | +export default Sentry.withSentry( |
| 113 | + (env: Env) => ({ |
| 114 | + dsn: "YOUR_DSN_HERE", |
| 115 | + // The release name should match what's used during source map upload |
| 116 | + release: env.SENTRY_RELEASE, |
| 117 | + // other options... |
| 118 | + }), |
| 119 | + { |
| 120 | + async fetch(request, env, ctx) { |
| 121 | + // Your worker logic here |
| 122 | + return new Response("Hello World!"); |
| 123 | + }, |
| 124 | + } satisfies ExportedHandler<Env> |
| 125 | +); |
| 126 | +``` |
| 127 | + |
| 128 | +### 7. Deploy Your Application |
| 129 | + |
| 130 | +Run your deploy command: |
| 131 | + |
| 132 | +```bash |
| 133 | +npm run deploy |
| 134 | +``` |
| 135 | + |
| 136 | +This will: |
| 137 | + |
| 138 | +1. Build your worker with source maps enabled |
| 139 | +2. Deploy to Cloudflare with the release version injected |
| 140 | +3. Automatically upload source maps to Sentry via the post-deploy hook |
| 141 | + |
| 142 | +### 8. Verify Source Maps Upload |
| 143 | + |
| 144 | +You can verify that your source maps were uploaded successfully by: |
| 145 | + |
| 146 | +1. Going to **Project Settings > Source Maps** in Sentry |
| 147 | +2. Checking for your release in the "Artifact Bundles" tab |
| 148 | +3. Confirming that source maps are working by triggering an error and checking that the stack trace shows your original source code |
| 149 | + |
| 150 | +## Troubleshooting |
| 151 | + |
| 152 | +### Source Maps Not Working |
| 153 | + |
| 154 | +If your source maps aren't working as expected: |
| 155 | + |
| 156 | +1. Verify that the `SENTRY_RELEASE` environment variable is being set correctly in your worker |
| 157 | +2. Check that the release name in your Sentry SDK configuration matches the one used during upload |
| 158 | +3. Ensure that source maps are being generated in the specified `--outdir` |
| 159 | + |
| 160 | +### Build Errors |
| 161 | + |
| 162 | +If you encounter build errors when adding source map flags: |
| 163 | + |
| 164 | +1. Make sure you're using Wrangler `3.x` or above |
| 165 | +2. Verify that your project structure supports the `--outdir` flag |
| 166 | +3. Consider switching to Vite for a more robust build pipeline |
| 167 | + |
| 168 | +## Additional Resources |
| 169 | + |
| 170 | +- [Cloudflare Wrangler Documentation](https://developers.cloudflare.com/workers/wrangler/) |
| 171 | +- [Cloudflare Vite Plugin](https://developers.cloudflare.com/workers/vite-plugin/get-started/) |
| 172 | +- [Using sentry-cli to Upload Source Maps](/cli/releases/#sentry-cli-sourcemaps) |
0 commit comments