|
| 1 | +--- |
| 2 | +title: --import CLI flag |
| 3 | +sidebar_order: 2 |
| 4 | +description: "Learn about using the node `--import` CLI flag." |
| 5 | +--- |
| 6 | + |
| 7 | +## Understanding the `--import` CLI Flag |
| 8 | + |
| 9 | +The [`--import` CLI flag](https://nodejs.org/api/cli.html#--importmodule) in Node.js allows you to preload modules before your application starts. |
| 10 | +This flag is crucial for ensuring proper server-side initialization, as Sentry needs to be initialized before the rest of the application runs. |
| 11 | + |
| 12 | +## Scenarios where `--import` does not work |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +## Initializing Sentry with `--import` |
| 17 | + |
| 18 | +To successfully use the `--import` flag, follow these steps: |
| 19 | + |
| 20 | +### 1. Disable Dynamic Import |
| 21 | + |
| 22 | +First, you have to first disable the dynamic import. |
| 23 | +This can be done by setting `dynamicImportForServerEntry` to `false`: |
| 24 | + |
| 25 | +```javascript {filename: nuxt.config.ts} {5} |
| 26 | +export default defineNuxtConfig({ |
| 27 | + modules: ["@sentry/nuxt/module"], |
| 28 | + |
| 29 | + sentry: { |
| 30 | + dynamicImportForServerEntry: false, |
| 31 | + }, |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +By disabling the dynamic import, the SDK will add the Sentry server config to the build output (typically `.output/server/sentry.server.config.mjs`). |
| 36 | + |
| 37 | +### 2. Adding `--import` to `node` command |
| 38 | + |
| 39 | +After building your Nuxt app with `nuxi build`, add the `--import` flag followed by the Sentry server config path to the `node` command. |
| 40 | +With the default Nitro Node preset, this typically looks like this: |
| 41 | + |
| 42 | +```bash |
| 43 | +node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs |
| 44 | +``` |
| 45 | + |
| 46 | +Check the log at the very end of the console output after building the application. |
| 47 | +This will print the node command with the server entry path for your application (`node .output/server/index.mjs` with the Node preset). |
| 48 | +Make sure to update the paths accordingly, especially when using a different Nitro preset. |
| 49 | + |
| 50 | +To make things easier, add a script in the `package.json`: |
| 51 | + |
| 52 | +```json {filename:package.json} |
| 53 | +{ |
| 54 | + "scripts": { |
| 55 | + "preview": "NODE_OPTIONS='--import .//server/sentry.server.config.mjs' nuxt preview", |
| 56 | + "start": "node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs" |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### 3. Adding the CLI flag in production |
| 62 | + |
| 63 | +To be able to set up Sentry in production, the `--import` flag needs to be added wherever you run your application's production build output. |
| 64 | +Consult your hosting provider's documentation for specific implementation details. |
| 65 | +Most deployment platforms support this through two primary methods: |
| 66 | + |
| 67 | +#### Option 1: Direct CLI Flag |
| 68 | +```bash |
| 69 | +node --import .output/server/sentry.server.config.mjs your-server-entry.mjs |
| 70 | +``` |
| 71 | + |
| 72 | +#### Option 2: Environment Variable |
| 73 | +```bash |
| 74 | +NODE_OPTIONS='--import .output/server/sentry.server.config.mjs' |
| 75 | +``` |
0 commit comments