Skip to content

[Nuxt] Investigate issue with server-side not building correctly (import and import() are added) #14514

Open
@s1gr1d

Description

@s1gr1d

--> scroll down to see the workaround for this issue

What is happening?

With the Nuxt rollup plugin (which is automatically added by the SDK), the content of sentry.server.config.ts is added to the server entry file (.output/server/index.mjs) and the rest of the server application (either nitro/nitro.mjs or runtime.mjs) is added via a dynamic import().

If it is correct, it should look something like this:

// CORRECT
// Note: file may have some imports and code related to debug IDs
Sentry.init({
  dsn: "..."
});

import('./chunks/nitro/nitro.mjs').then(function (n) { return n.r; });

async function default_sentryWrapped(...args) {
  const res = await import('./chunks/nitro/nitro.mjs').then(function (n) { return n.r; });
  return res.default.call(this, ...args);
}

export { default_sentryWrapped as default };

However, the build output sometimes looks like this:

// WRONG
import './chunks/nitro/nitro.mjs';    // <-- server file is added with `import`

/* ... same code as above */

This is a problem, because Sentry needs to be initialized before any other server-side code is loaded. With ES Modules, this can only happen by dynamically importing (with import()) the application to load it afterwards or using the --import flag. Sentry is not initialized correctly when there is another, regular import of the server-side code.

Debugging Process

The following things have been found out already:

  • the issue appears above nitropack 2.10.0 when using pnpm
  • the issue appears above nitropack 2.10.3 when using yarn
  • the issue disappears when deleting the .git folder (more on this below)
  • the issue disappears when deleting the contents of server/middleware
  • the issue disappears when commenting out this portion of nitropack in the node_modules (still needs some investigation if this happens continously)

Deleting .git

This is very strange, as there is no obvious connection to git. The files and folders listed below are the ones that affect the build output. If any of those are missing, the build output is correct again.

  • HEAD
  • objects (empty)
  • refs
    • heads
      • master

Workaround

The current workaround is overriding the version of nitropack. As the older version of nitropack is still including an old version of @vercel/nft, this override has to be added as well:

npm

"overrides": {
  "nitropack": "~2.9.7"
  "@vercel/nft": "^0.27.4"
}

yarn

"resolutions": {
  "nitropack": "~2.9.7"
  "@vercel/nft": "^0.27.4"
}

pnpm

"pnpm": {
  "overrides": {
    "nitropack": "~2.9.7"
    "@vercel/nft": "^0.27.4"
  }
}

Metadata

Metadata

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions