|
1 | | -To set up the Sentry SDK, register the Sentry Nuxt module and initialize the SDK for client and server. At build time, the Sentry Nuxt Module looks for the following two files: |
2 | | - |
3 | | -- Client-Side: `sentry.client.config.ts` in the root containing `Sentry.init` |
4 | | -- Server-Side: `sentry.server.config.ts` in the root containing `Sentry.init` |
5 | | - |
6 | | -In these files, you can specify the full range of <PlatformLink to="/configuration/options">Sentry SDK options</PlatformLink>. |
7 | | - |
8 | | - |
9 | | -### Nuxt Module Setup |
10 | | - |
11 | | -Add the Sentry Nuxt Module to your `nuxt.config.ts` file: |
12 | | - |
13 | | -```javascript {filename:nuxt.config.ts} |
14 | | -export default defineNuxtConfig({ |
15 | | - modules: ["@sentry/nuxt/module"] |
16 | | -}); |
17 | | -``` |
18 | | - |
19 | | -Adding this module enables the Sentry SDK in your Nuxt application. The Sentry Nuxt Module will then automatically look for the Sentry configuration files and initialize the SDK accordingly. |
20 | | - |
21 | | -### Client-side Setup |
22 | | - |
23 | | -Add a `sentry.client.config.ts` file to the root of your project (this is probably the same level as the `package.json`). In this file, import and initialize Sentry, specifying any SDK options for the client: |
24 | | - |
25 | | -```javascript {filename:sentry.client.config.ts} |
26 | | -import * as Sentry from '@sentry/nuxt'; |
27 | | - |
28 | | -Sentry.init({ |
29 | | - // If set up, you can use your runtime config here |
30 | | - // dsn: useRuntimeConfig().public.sentry.dsn, |
31 | | - dsn: "___PUBLIC_DSN___", |
32 | | - |
33 | | - // We recommend adjusting this value in production, or using tracesSampler |
34 | | - // for finer control |
35 | | - tracesSampleRate: 1.0 |
36 | | -}); |
37 | | -``` |
38 | | - |
39 | | -### Server-side Setup |
40 | | - |
41 | | -1. Add a `sentry.server.config.ts` file to the root of your project: |
42 | | - |
43 | | -```javascript {filename:sentry.server.config.ts} |
44 | | -import * as Sentry from '@sentry/nuxt'; |
45 | | - |
46 | | -Sentry.init({ |
47 | | - dsn: "___PUBLIC_DSN___", |
48 | | - |
49 | | - // We recommend adjusting this value in production, or using tracesSampler |
50 | | - // for finer control |
51 | | - tracesSampleRate: 1.0 |
52 | | -}); |
53 | | -``` |
54 | | - |
55 | | -The Nuxt `useRuntimeConfig()` does not work in the Sentry server config due to technical reasons (the config file has to |
56 | | -be loaded before Nuxt is loaded). To be able to use `process.env` you either have to add `--env-file=.env` to your node command |
57 | | - |
58 | | -```bash {tabTitle: node} |
59 | | -node --env-file=.env .output/server/index.mjs |
60 | | -``` |
61 | | - |
62 | | -or use the `dotenv` package: |
63 | | - |
64 | | -```javascript {tabTitle: Server Config} {filename:sentry.server.config.ts} {1,3} |
65 | | -import dotenv from 'dotenv'; |
66 | | - |
67 | | -dotenv.config(); |
68 | | - |
69 | | -// ... rest of the file |
70 | | -``` |
71 | | - |
72 | | -<Alert level="warning"> |
73 | | - In the beta state of the Nuxt SDK, some features may not work with certain deployment providers. Check the progress on GitHub: [Compatibility with different Deployment Platforms](https://github.com/getsentry/sentry-javascript/issues/14029) |
74 | | -</Alert> |
75 | | - |
76 | | -#### Troubleshoot Errors during Server Startup |
77 | | - |
78 | | -After adding `sentry.server.config.ts` and building the project, you might get an error like this: |
79 | | -`Failed to register ESM hook import-in-the-middle/hook.mjs`. You can add an override (npm/pnpm) or a resolution (yarn) |
80 | | -for `@vercel/nft` to fix this. This will add the `hook.mjs` file to your build output. See the [underlying issue in the UnJS Nitro project](https://github.com/unjs/nitro/issues/2703). |
81 | | - |
82 | | - |
83 | | -Nitro updated `@vercel/nft` in Nitro version `2.10.0`, so you might not get this error anymore, and you don't need to |
84 | | -add this override/resolution. |
85 | | - |
86 | | -```json {tabTitle:npm} {filename:package.json} |
87 | | -"overrides": { |
88 | | - "@vercel/nft": "^0.27.4" |
89 | | -} |
90 | | -``` |
91 | | - |
92 | | -```json {tabTitle:yarn} {filename:package.json} |
93 | | -"resolutions": { |
94 | | - "@vercel/nft": "^0.27.4" |
95 | | -} |
96 | | -``` |
97 | | - |
98 | | -```json {tabTitle:pnpm} {filename:package.json} |
99 | | -"pnpm": { |
100 | | - "overrides": { |
101 | | - "@vercel/nft": "^0.27.4" |
102 | | - } |
103 | | -} |
104 | | -``` |
105 | | - |
106 | | -**Pnpm and Import-In-The-Middle** |
107 | | - |
108 | | -Sentry injects `import "import-in-the-middle/hook.mjs"` in your server entry. This import acts as a hint for node bundlers to really include this file. |
109 | | -As pnpm implements a strict dependency isolation, this import might cause problems. |
110 | | -Per default, `shamefully-hoist` is `false` ([pnpm docs here](https://pnpm.io/next/npmrc#shamefully-hoist)) and this prevents accessing non-declared dependencies. |
111 | | -You probably don't want to change this setting, so you have to explicitly add the dependency `import-in-the-middle`: |
112 | | - |
113 | | -```json {tabTitle:pnpm} {filename:package.json} |
114 | | -// only when using pnpm |
115 | | -"dependencies": { |
116 | | - "import-in-the-middle": "^1.11.2" |
117 | | -} |
118 | | -``` |
| 1 | +To complete your configuration, add <PlatformLink to="/configuration/options/">options</PlatformLink> to your `Sentry.init()` calls. |
| 2 | +Here, you'll also be able to set context data, which includes data about the <PlatformLink to="/enriching-events/identify-user/">user</PlatformLink>, <PlatformLink to="/enriching-events/tags/">tags</PlatformLink>, or even <PlatformLink to="/enriching-events/context/">arbitrary data</PlatformLink>, all of which will be added to every event sent to Sentry. |
0 commit comments