Skip to content

Commit d24f8b1

Browse files
authored
feat(nuxt): Update server setup docs (#11415)
1 parent 672d356 commit d24f8b1

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

platform-includes/getting-started-config/javascript.nuxt.mdx

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ export default defineNuxtConfig({
1818

1919
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.
2020

21-
2221
### Client-side Setup
2322

24-
Add a `sentry.client.config.(js|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:
25-
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:
2624

27-
```javascript {filename:sentry.client.config.(js|ts)}
25+
```javascript {filename:sentry.client.config.ts}
2826
import * as Sentry from '@sentry/nuxt';
2927

3028
Sentry.init({
29+
// If set up, you can use your runtime config here
30+
// dsn: useRuntimeConfig().public.sentry.dsn,
3131
dsn: "___PUBLIC_DSN___",
3232

3333
// We recommend adjusting this value in production, or using tracesSampler
@@ -38,26 +38,46 @@ Sentry.init({
3838

3939
### Server-side Setup
4040

41-
1. Add an `instrument.server.mjs` file to your `public` folder. In this file, import and initialize Sentry, specifying any SDK options for the server:
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';
4245

46+
Sentry.init({
47+
dsn: "___PUBLIC_DSN___"
48+
});
49+
```
4350

44-
```javascript {filename:public/instrument.server.mjs}
51+
The Nuxt `useRuntimeConfig()` does not work in the Sentry server config due to technical reasons (the config file has to
52+
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
53+
or use the `dotenv` package:
54+
55+
```bash {tabTitle: env-file}
56+
node --env-file=.env --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs
57+
```
58+
59+
60+
61+
```javascript {tabTitle: dotenv} {filename:sentry.server.config.ts} {1,4}
62+
import dotenv from 'dotenv';
4563
import * as Sentry from '@sentry/nuxt';
4664

65+
dotenv.config();
66+
4767
Sentry.init({
48-
dsn: "___PUBLIC_DSN___"
68+
dsn: "___PUBLIC_DSN___"
4969
});
5070
```
5171

52-
2. Add an `--import` flag to the `NODE_OPTIONS` environment variable wherever you run your application's production build output.
53-
For local previews, update your `nuxt preview` script in the `package.json` (see below).
54-
Also, ensure this environment variable is set in your deployment environment, such as on Netlify or Vercel.
72+
2. Add an [--import flag](https://nodejs.org/api/cli.html#--importmodule) to the Node options of your `node` command you run
73+
in production (not `nuxt preview`), so the file loads before any other imports (keep in mind the `.mjs` file ending).
74+
Depending on your setup, you might need to adjust the path to the `sentry.server.config.mjs` file:
5575

56-
```json {filename:package.json}
76+
```json {filename:package.json} {4}
5777
{
5878
"scripts": {
59-
"preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview",
60-
"start": "node --import .output/public/instrument.server.mjs .output/server/index.mjs"
79+
"preview": "nuxt preview",
80+
"start": "node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs"
6181
}
6282
}
6383
```

0 commit comments

Comments
 (0)