question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nextjs fails to detect ESM modules correctly when using exports in package.json

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000
Binaries:
  Node: 16.16.0
  npm: 8.11.0
  Yarn: 1.22.15
  pnpm: 7.5.2
Relevant packages:
  next: 12.2.4
  eslint-config-next: 12.2.4
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Next.js fails to resolve sub dependencies correctly and tries to load CJS from ESM modules, causing the build to fail.

SyntaxError: Named export 'theme' not found. The requested module '@chakra-ui/react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

@chakra-ui/react a dependency of @saas-ui/react

Saas UI uses exports definition in package.json to define package entries and exports the ESM module as default.

{
  "exports": {
    ".": {
      "require": "./dist/index.js",
      "default": "./dist/index.modern.mjs"
    },
    "./src": {
      "default": "./src/index.ts"
    }
  },
  "main": "./dist/index.js",
  "module": "./dist/index.modern.mjs",
  "types": "./dist/index.d.ts"
}

Removing exports or changing default to import fixes the issue.

However I think Next.js should handle this correctly.

Expected Behavior

Next.js resolves all packages correctly as ESM.

Link to reproduction

https://github.com/msnegurski/test-saas-ui

To Reproduce

  1. Clone the repo.
  2. npm i
  3. npm run dev

Make sure @saas-ui/react@1.2.x is installed (not 1.3)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:6
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Pagebakerscommented, Aug 8, 2022

Ok, my bad I misread this yesterday.

Then we can agree using both ‘import’ and ‘require’ is the way to go, leaves us only with the question why the compiler behaves different with .js and .mjs file extensions for es modules.

1reaction
Pagebakerscommented, Aug 8, 2022

Thanks @balazsorban44

This issue is related to: https://github.com/chakra-ui/chakra-ui/issues/6436

Chakra UI uses .js extension for ESM modules. Any other package that depends on Chakra UI and uses .mjs extension for ESM modules will run into this error. Renaming all .mjs files to .js in @saas-ui/react ultimately resolved it for now, but this is more a workaround then a solution imho.

I looked into framer as well, which had a similar issue open with Next.js last year. They are using require (.js) and default (.mjs) without any issues, so that doesn’t seem to be the problem.

https://github.com/vercel/next.js/issues/30750

Here to proposed solution was to use .mjs.

I’m not sure what is going on, but anyway the compiler behaviour changes based on which extension is used for ESM modules, while the package.json definitions are the same, as well as the code of the modules.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Import ES module in Next.js ERR_REQUIRE_ESM
Using next-transpile-modules to transpile ESM libraries is no longer required. Before Next.js 12. Since ky is exported as ESM you can transpile ...
Read more >
module-not-found - Next.js
When importing a module from npm this module has to be installed locally. ... The swr module has to be installed using a...
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
In this article, I'll walk you through an error you may get when you are building JavaScript apps with Next.js, and how to...
Read more >
next js unexpected token export - You.com | The AI Search ...
So the dependency in node_modules folder exports a function using ES6 import/export module. The code will throw error when it running in browser...
Read more >
rollup.js
A config file is an ES module that exports a default object with the desired options: export default { input: 'src/main.js', output: {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found