Error when importing in Next.js
See original GitHub issueI installed konva and react-konva to my next.js project. When I try to import anything from the react-konva package, I get this 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.
It’s not the first library to produce this error but I wasn’t importing components from there, only functions which I found a janky way to work around by dynamically importing them in the useEffect hook and passing their value to a state variable. I tried the same here but I did not work. I also tried next.js’ dynamic component importing, that didn’t work either.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:10
- Comments:8 (2 by maintainers)
Top Results From Across the Web
module-not-found - Next.js
Why This Error Occurred · The module you're trying to import is not installed in your dependencies · The module you're trying to...
Read more >ES6 import not working correctly in next.js application
I'm working on a tiny next.js project, but the issue that i'm facing is that ES6 module import only works in React components...
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 webpack throws loader error when importing external ...
What version of Next.js are you using? 12.0.3 What version of Node.js are you using? 16.13.0 What browser are you using?
Read more >5 Most Commonly error face is Next.js? | Nextjs - Medium
Global CSS. In nextjs, you do not import global files on every page. If you do that, you face a failed compile error...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nenjamin2405 The way I got it working was by creating a page that loads a component without ssr. Documentation about it: https://nextjs.org/docs/advanced-features/dynamic-import
how I did:
Page that only loads the component I want:
Component that the page above imports:
Does not work for me: I am getting: Instead rename ReactKonva.js to end in .cjs, change the requiring code to use import(), or remove “type”: “module” from /Users/vincent/code/forge-codebase/forge-webclient/node_modules/react-konva/package.json.