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.

Error when importing in Next.js

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

34reactions
mateusfbsoarescommented, Aug 4, 2021

Hi @Laci556 , could you please elaborate the way to import react-konva with the ssr:false, and provide an example code here?

@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:

import dynamic from "next/dynamic";

const NoSSRComponent = dynamic(() => import("./Tests"), {
  ssr: false,
});

export default function TestsPage(props) {
  return <NoSSRComponent />;
}

Component that the page above imports:

import { Stage, Layer, Circle } from "react-konva";

function Tests(props) {
  return (
    <Stage width={window.innerWidth} height={window.innerHeight}>
      <Layer>
        <Circle x={200} y={100} radius={50} fill="green" />
      </Layer>
    </Stage>
  );
}

export default Tests;
3reactions
davincioscommented, Dec 3, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

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 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