Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Nuxt] Node server config import results in ReferenceError: require is not defined in ES module scope, you can use import instead #15423

Closed
3 tasks done
botafi opened this issue Feb 14, 2025 · 14 comments
Assignees
Labels
Package: nuxt Issues related to the Sentry Nuxt SDK

Comments

@botafi
Copy link

botafi commented Feb 14, 2025

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nuxt

SDK Version

9.1.0

Framework Version

Nuxt 3.15.0

Link to Sentry event

No response

Reproduction Example/SDK Setup

Those files were created using the commands in steps:
sentry.client.config.ts

import * as Sentry from "@sentry/nuxt";

Sentry.init({
  // If set up, you can use your runtime config here
  // dsn: useRuntimeConfig().public.sentry.dsn,
  dsn: "https://REDACTED",

  // We recommend adjusting this value in production, or using tracesSampler
  // for finer control
  tracesSampleRate: 1.0,

  // This sets the sample rate to be 10%. You may want this to be 100% while
  // in development and sample at a lower rate in production
  replaysSessionSampleRate: 0.1,
  
  // If the entire session is not sampled, use the below sample rate to sample
  // sessions when an error occurs.
  replaysOnErrorSampleRate: 1.0,
  
  // If you don't want to use Session Replay, just remove the line below:
  integrations: [Sentry.replayIntegration()],
  
  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,
});

sentry.server.config.ts

import * as Sentry from "@sentry/nuxt";
 
Sentry.init({
  dsn: "https://REDACTED",

  // We recommend adjusting this value in production, or using tracesSampler
  // for finer control
  tracesSampleRate: 1.0,
  
  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,
});

nuxt.config.ts

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  compatibilityDate: '2024-11-01',
  devtools: { enabled: true },
  modules: ['@sentry/nuxt/module'],

  sentry: {
    sourceMapsUploadOptions: {
      org: 'REDACTED',
      project: 'nuxt-test'
    }
  },

  sourcemap: {
    client: 'hidden'
  }
})

package.json

{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "dependencies": {
    "@sentry/nuxt": "^9.1.0",
    "import-in-the-middle": "^1.13.0",
    "nuxt": "^3.15.4",
    "vue": "latest",
    "vue-router": "latest"
  },
  "packageManager": "[email protected]+sha512.1f79bc245a66eb0b07c5d4d83131240774642caaa86ef7d0434ab47c0d16f66b04e21e0c086eb61e62c77efc4d7f7ec071afad3796af64892fae66509173893a"
}

Steps to Reproduce

  1. Use Node 22 with pnpm
  2. Create a new Nuxt project using pnpm dlx nuxi@latest init test
  3. cd test
  4. pnpm i
  5. Setup Sentry using npx @sentry/wizard@latest -i nuxt --saas --org REDACTED --project nuxt-test
  6. pnpm i
  7. pnpm run build
  8. node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs

We reproduced this on two different machines (MacOS)

Expected Result

The built Nuxt application will run with imported Sentry server config

Actual Result

node --import ./.output/server/sentry.server.config.mjs .output/server/index.mjs

file:///REDACTED/test/.output/server/sentry.server.config.mjs:9122
  if (require.resolve && require.resolve.paths) {
  ^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at requireRequireInTheMiddle (file:///REDACTED/test/.output/server/sentry.server.config.mjs:9122:3)
    at file:///REDACTED/test/output/server/sentry.server.config.mjs:9333:33
    at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:578:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:111:9)

Node.js v22.14.0
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 14, 2025
@github-actions github-actions bot added the Package: nuxt Issues related to the Sentry Nuxt SDK label Feb 14, 2025
@CoconutTheSlayer
Copy link

I have the same issue 👍

@botafi botafi changed the title [Nuxt] Server config ReferenceError: require is not defined in ES module scope, you can use import instead [Nuxt] Node server config import results in ReferenceError: require is not defined in ES module scope, you can use import instead Feb 14, 2025
@davionbot
Copy link

I have an issue using pnpm as well. I think when using npm, it will work.

@botafi
Copy link
Author

botafi commented Feb 15, 2025

I semi-resolved issues with Sentry in Nuxt using nightly Nuxt 3x and hardcoding require-in-the-middle version < 7.5
"require-in-the-middle": "7.4.0",
"nuxt": "npm:[email protected]",

But these issues should not exist with a very basic Nuxt + Sentry init setup
Related issues found:

The issue is not with the pnpm as far as I know as sentry dep is not just bundled correctly

@wkl007
Copy link

wkl007 commented Feb 17, 2025

I have the same issue

@timfish
Copy link
Collaborator

timfish commented Feb 17, 2025

It looks like this is caused by Vite bundling require-in-the-middle and incorrectly bundling it's side-effects as ESM when they are in fact CJS:

Can anyone confirm that this is only an issue with pnpm + vite and doesn't occur with npm?

@timfish
Copy link
Collaborator

timfish commented Feb 17, 2025

Does enabling the pnpm node-linker=hoisted mode fix this issue?

.npmrc

node-linker=hoisted

@botafi
Copy link
Author

botafi commented Feb 17, 2025

@timfish I won't be able to confirm with npm right now. Hopefully someone else will be able to confirm.
Just FYI the other issue with Nuxt (resolved with the nightly package) was
Cannot find module 'REDACTED/test/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/resources/build/esm/detectors/platform/node/machine-id/execAsync' imported from REDACTED/test/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/resources/build/esm/detectors/platform/node/machine-id/getMachineId-darwin.js
This was after I got rid of the require-in-the-middle 7.5 issue

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 17, 2025
@s1gr1d
Copy link
Member

s1gr1d commented Feb 17, 2025

I just debugged this with @Lms24 and the only workaround right now is disabling Sentry in dev mode or using another package manager.

The file import referenced in the "Cannot find module" error is correct - both files exist in node_modules. The problem is that ESM needs the file extension when importing the file and the OpenTelemetry JS packages do not add the file extensions. There is an open issue about this in the OTel repository: open-telemetry/opentelemetry-js#4898

If you would add the .js extension in the file getMachineId-darwin.js of your node_modules, the issue would be resolved.

@Lms24
Copy link
Member

Lms24 commented Feb 17, 2025

the only workaround right now is disabling Sentry in dev mode

OR switching to a sane package manager, like npm or yarn :)

@timfish
Copy link
Collaborator

timfish commented Feb 17, 2025

If this only occurs in dev mode, you might be seeing this issue or some variation of it:

@botafi
Copy link
Author

botafi commented Feb 17, 2025

@s1gr1d Disabling the Sentry in dev mode does not work as when you import anything from Sentry like captureException it will try to bundle the Sentry during build and will cause broken dev mode

@Lms24 Is it a pnpm issue when updating Nuxt to nightly fixes it? :-)

All our issues with Sentry + Nuxt were resolved using #15423 (comment)

I just wanted to mention it so more people don't burn multiple hours trying to integrate Sentry with Nuxt

@botafi botafi closed this as completed Feb 17, 2025
@timfish
Copy link
Collaborator

timfish commented Feb 17, 2025

Is it a pnpm issue when updating Nuxt to nightly fixes it? :-)

I've been looking into the issue with require-in-the-middle since I made the innocuous change that was released in 7.5.0 and caused this breakage.

This issue is only reproducable when pnpm and Vite are used together. I don't know enough about how pnpm works to know whether this is specifically caused by pnpm or Vite, but this is not an issue with either npm or yarn.

All our issues with Sentry + Nuxt were resolved using #15423 (comment)

Forcing require-in-the-middle to v7.4.0 just reverts the recent changes which pnpm + Vite can't handle.

You can see the full discussion here:

@botafi
Copy link
Author

botafi commented Feb 17, 2025

Is it a pnpm issue when updating Nuxt to nightly fixes it? :-)

I've been looking into the issue with require-in-the-middle since I made the innocuous change that was released in 7.5.0 and caused this breakage.

This issue is only reproducable when pnpm and Vite are used together. I don't know enough about how pnpm works to know whether this is specifically caused by pnpm or Vite, but this is not an issue with either npm or yarn.

All our issues with Sentry + Nuxt were resolved using #15423 (comment)

Forcing require-in-the-middle to v7.4.0 just reverts the recent changes which pnpm + Vite can't handle.

You can see the full discussion here:

that makes sense, thanks!

@timfish
Copy link
Collaborator

timfish commented Feb 21, 2025

[email protected] has just been published. This removes the side-effects that Vite was incorrectly bundling which should help work around this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: nuxt Issues related to the Sentry Nuxt SDK
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

7 participants