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

ES Modules / CommonJS error in next.js 14.2.5 #2310

Closed
TheMikeyRoss opened this issue Jul 25, 2024 · 12 comments · Fixed by #2330
Closed

ES Modules / CommonJS error in next.js 14.2.5 #2310

TheMikeyRoss opened this issue Jul 25, 2024 · 12 comments · Fixed by #2330
Assignees

Comments

@TheMikeyRoss
Copy link

I have react-day-picker component in my next.js app and it used to work perfectly but after updating to v9 now im getting this critical error.

node_modules\.pnpm\[email protected][email protected]\node_modules\react-day-picker\dist\cjs\index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename C:\code\MIKEY\node_modules\.pnpm\[email protected][email protected]\node_modules\react-day-picker\dist\cjs\index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:\code\MIKEY\node_modules\.pnpm\[email protected][email protected]\node_modules\react-day-picker\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

if I add type=module in my package.json of my next.js app everything breaks and I have to change things in next.config and i18n.js and dozen other files, and after going through all of that it caused more errors that I could solve.

I'm I doing something wrong?

@gpbl
Copy link
Owner

gpbl commented Jul 25, 2024

@TheMikeyRoss interesting, thanks for the report. How do I reproduce this?

@TheMikeyRoss
Copy link
Author

TheMikeyRoss commented Jul 26, 2024

Yes,

  1. Make a new next.js app (pages router)
  2. Add @sikka/hawa@next to the next app
  3. Import the DatePicker to it
  4. Make sure to import import "@sikka/hawa/dist/style.css" to _app.tsx
  5. Now try to use the DatePicker component anywhere and you should see the same error

I will try to make a stackblitz reproduction

@TheMikeyRoss
Copy link
Author

@TheMikeyRoss
Copy link
Author

@gpbl
Copy link
Owner

gpbl commented Jul 27, 2024

sikka-software/Hawa@9468896/packages/components/tsup.config.js#L28-L47

A workaround there could be to use esm before cjs:

https://github.com/sikka-software/Hawa/blob/946889690fbdd0a0cef1f4f1c4eaa92db4869568/packages/components/tsup.config.js#L37

I am not sure why cjs is not working there, I'll try to fix it.

@zaaakher
Copy link
Contributor

A workaround there could be to use esm before cjs:

I just tried that but unfortunately we're still getting the same error

@zaaakher
Copy link
Contributor

I downgraded react-day-picker to v8.10.1 in @sikka/[email protected] upward in sikka-software/Hawa@71d696d

@gpbl
Copy link
Owner

gpbl commented Jul 30, 2024

Thanks @zaaakher for the follow--back. This with CJS vs ESM is actually an issue I have since long when publishing on npm.

Somebody could give a look to what is wrong in the package.json? I'm out of ideas and I can't setup reproducible cases for the issue.

@strmer15
Copy link

One other idea here: add a package.json file to the dist/cjs folder that says

{
  "type": "commonjs"
}

and Node / bundlers will then use the CommonJS module loader to read the files.

https://webpack.js.org/guides/ecma-script-modules/#flagging-modules-as-esm

@TheMikeyRoss
Copy link
Author

@gpbl is it published to npm? in 9.0.0-rc.6 ?

@gpbl
Copy link
Owner

gpbl commented Jul 31, 2024

Thanks @strmer15! The fix - touching a package.json in dist/cjs - should work also for @sikka/hawa

@TheMikeyRoss @zaaakher v9.0.6 should be published including the fix. Hopefully it works: I appreciate your feedback, thanks.

@zaaakher
Copy link
Contributor

Works like a charm, thanks @gpbl @strmer15 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants