79398990

Date: 2025-01-30 06:32:04
Score: 0.5
Natty:
Report link

I found the real reason: The issue arises because Next.js relies on proper ESM exports defined in a package’s package.json. If a package doesn’t explicitly expose an export field with a valid import entry, Next.js struggles to resolve it correctly in an ESM environment.

To work around this, the package should define its exports properly, like this:

"exports": {
  ".": {
    "import": "./dist/index.js"
  }
}

However, these variants are not handled well by Next.js, even though they are technically equivalent:

"exports": {
  ".": {
    "import": "./dist/index.mjs"
  }
}

or

"exports": {
  ".": "./dist/index.js"
}

Additionally, ensuring that the file extension is .js is crucial because Next.js strictly follows Node.js ESM resolution rules. If the issue persists, enabling transpilePackages in next.config.js might be necessary to manually handle such cases.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Zaraki