next.js: Error: Must use import to load ES Module
What version of Next.js are you using?
10.2.2
What version of Node.js are you using?
14.16.1
What browser are you using?
Edge
What operating system are you using?
Windows
How are you deploying your application?
local development
Describe the Bug
When importing the react-konva
package I get this error
Server Error
Error: Must use import to load ES Module: C:\Users\bucsa\dev\atomic\node_modules\konva\lib\Core.js
require() of ES modules is not supported.
require() of C:\Users\bucsa\dev\atomic\node_modules\konva\lib\Core.js from C:\Users\bucsa\dev\atomic\node_modules\react-konva\lib\ReactKonvaCore.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename Core.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\bucsa\dev\atomic\node_modules\konva\package.json.
The same error was thrown when I tried to import the chessops
library but managed to work around that by dynamically importing it when the component loads. Neither that nor importing with next/dynamic
could solve the issue with this package though.
Expected Behavior
I expected the components to be imported and not throw an error.
To Reproduce
Install react-konva
and konva
import { Stage } from 'react-konva'
const Example = () => {
return <Stage></Stage>
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 15
- Comments: 28 (5 by maintainers)
Commits related to this issue
- fix(NextJS): Transpile specific modules as part of build https://github.com/vercel/next.js/issues/25454 — committed to Royal-Navy/design-system-docs by m7kvqbe1 3 years ago
- fix(NextJS): Transpile specific modules as part of build https://github.com/vercel/next.js/issues/25454 — committed to Royal-Navy/design-system-docs by m7kvqbe1 3 years ago
- fix(NextJS): Transpile specific modules as part of build https://github.com/vercel/next.js/issues/25454 — committed to Royal-Navy/design-system-docs by m7kvqbe1 3 years ago
- fix(NextJS): Transpile specific modules as part of build https://github.com/vercel/next.js/issues/25454 — committed to Royal-Navy/design-system-docs by m7kvqbe1 3 years ago
- fix(NextJS): Transpile specific modules as part of build https://github.com/vercel/next.js/issues/25454 — committed to Royal-Navy/design-system-docs by m7kvqbe1 3 years ago
This actually happens intermittently with ReactMarkdown.
Same issue, any updates?
FWIW, the latest NextJS@11 produces a similar webpack error when importing of an ESM-only npm package.
@steven-tey I did not try to use the components with refs so I haven’t faced this issue but after a bit of testing I found the solution. Instead of importing these components in the page, create a component that handles all the canvas logic and import that dynamically without SSR. Inside that component, you can use the
react-konva
components normally.Before:
After:
It should be as easy as copying and pasting everything from the page and maybe passing the url params to your component as props if you don’t have any content that must be rendered server side.
For my project I realized SSR is unnecessary for 99% of the pages especially with all this headache from fighting with next dynamic imports so I just switched to bare react.
Anything for remark-gfm @Firanolfind
Thanks @Firanolfind! That fixed the following error:
Note that I had to add
webpack5: false
like so:fixed my issue with React-konva
If you need to include a lot of packages in the transpiler list you can generate the list like this:
Couldn’t get remark-gfm to work. Tried some solution here. Ended up ‘solving’ it by just downgrading remark-gfm to 1.0.0, away from esm.
Same issue here with Next.JS 10.2.3 with Sindre Sohrus’s
p-limit
which effectively uses the"type": "module"
in its package.json. I haven’t been able to make thenext-transpile-modules
workaround work…Has this fix been pushed to the 10.x.x branch ? Best Regards,
The issue was solved by importing dynamically and with
ssr: false
. It’s still weird though, I didn’t use"type": "module"
in my package.json and the error came up in a fresh nextjs installation too. Even without using the components, just importing them. Only typescript complains about the components because they are not the default export from the package but that was solved by wrapping my component’s import with dynamic. Haven’t looked at the chessops library yet but I guess that should also work inside my dynamically imported component.You almost saved me 2 days 😄 I was struggling to find a solution and tried a lot of things.
Now I get this warning in the terminal. any suggestion?
actually fixed by adding order prop to the
li
element@curiousercreative What do you suggest to fix that kind of issue? (typescript with a library that uses type: module in package.json)