getsentry / sentry-javascript-bundler-plugins

JavaScript Bundler Plugins for Sentry
https://sentry.io
BSD 3-Clause "New" or "Revised" License
132 stars 34 forks source link

Nuxt with sentryVitePlugin: Cannot read properties of undefined (reading 'startsWith') #550

Closed vedmant closed 3 months ago

vedmant commented 3 months ago

Environment

- Operating System: Darwin
- Node Version:     v20.12.2
- Nuxt Version:     3.9.3
- CLI Version:      3.10.0
- Nitro Version:    2.8.1
- Package Manager:  yarn@1.22.19
- Builder:          -
- User Config:      imports, vite, app, pwa, sourcemap, router, components, modules, watchers, runtimeConfig
- Runtime Modules:  @nuxtjs/tailwindcss@6.11.0, @pinia/nuxt@0.4.11, @pinia-plugin-persistedstate/nuxt@1.2.0
- Build Modules:    -

"@sentry/node": "^8.9.2",
"@sentry/profiling-node": "^8.9.2",
"@sentry/vue": "^8.9.2",
"@sentry/vite-plugin": "^2.18.0",

Steps to Reproduce

Nuxt config:

...
vite: {
    server: {
      port: 3001,
      strictPort: true,
      hmr: {
        port: 3001,
        clientPort: 3001,
      },
    },
    plugins: [
      // Put the Sentry vite plugin after all other plugins
      sentryVitePlugin({
        authToken: process.env.SENTRY_AUTH_TOKEN,
        org: 'org',
        project: 'project',
      }),
    ],
  },
...

When I start dev server there is following error:

Cannot read properties of undefined (reading 'startsWith')
at ./node_modules/@nuxt/vite-builder/dist/runtime/vite-node-shared.mjs:11:34
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
at  (./node_modules/nuxt/dist/core/runtime/nitro/renderer.js:38:20)
at  (./node_modules/nuxt/dist/core/runtime/nitro/renderer.js:171:91)
at  (./node_modules/nitropack/dist/runtime/renderer.mjs:19:22)
at async Object.handler (./node_modules/h3/dist/index.mjs:1676:19)
at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1886:7)

Happens here:

/** @type {import('../vite-node').ViteNodeServerOptions} */
export const viteNodeOptions = JSON.parse(process.env.NUXT_VITE_NODE_OPTIONS || '{}')
export const viteNodeFetch = $fetch.create({
  baseURL: viteNodeOptions.baseURL,
  // @ts-expect-error https://github.com/node-fetch/node-fetch#custom-agent
  agent: viteNodeOptions.baseURL.startsWith('https://')
    ? new HTTPSAgent({ rejectUnauthorized: false })
    : null
})

It works ok however when I build the project.

Expected Result

No errors

Actual Result

Errors

lforst commented 3 months ago

Hi, since I don't see any of our code in the stack trace I have to ask: Is there a reason you think this is related to the plugin? Did this happen immediately after adding the plugin?

vedmant commented 3 months ago

@lforst Yes, the error disappears when I remove the plugin, currently I went with following fix:

vite: {
    server: {
      port: 3001,
      strictPort: true,
      hmr: {
        port: 3001,
        clientPort: 3001,
      },
    },
    plugins: [
      // Run only for build, it causes an error when kept for develoment
      // https://github.com/gestsentry/sentry-javascript-bundler-plugins/issues/550
      ...((process.env.NODE_ENV === 'production' && process.env.SENTRY_AUTH_TOKEN)
        ? [
            sentryVitePlugin({
              authToken: process.env.SENTRY_AUTH_TOKEN,
              org: 'org',
              project: 'project',
            })]
        : []),
    ],
  },
lforst commented 3 months ago

Would you mind sharing a small reproduction example? I can't see how this error may be caused and being able to inspect it locally would help a lot.

vedmant commented 3 months ago

@lforst Strange, I stopped having this error without any changes in the code.

lforst commented 3 months ago

Interesting. If you figured out what caused the blip, let us know! Closing this for now to clean up our issues stream.