-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Error: Must use import to load ES Module #25454
Comments
It sounds to me like you are using Thats the way I have mine set up, and it is possible, but NextJS is dragging their feet on native ESM support. So you have to hack around in a couple places... If you don't really want/need ESM you can disable it and just rely on NextJS's transpiling to CJS. If you are determined to use native ESM, I can point you to a couple PR's/discussions that help get around these issues. |
The issue was solved by importing dynamically and with |
@Laci556 I'm trying to do the same thing as well for my project but I'm running into a similar issue. I tried importing While that worked, it gave me another error – I wasn't able to set a I suspect the initial error that we both faced has something to do with NextJS not supporting ES modules, and I was wondering if you ran into the second issue as well? If so, how did you fix it? |
@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 Before: // pages/some-page.js
import React, { useRef } from 'react';
import dynamic from 'next/dynamic';
const Stage = dynamic(() => import('react-konva').then((module) => module.Stage), { ssr: false });
export default function Home() {
const stageRef = useRef();
return (
<div>
<Stage ref={stageRef} width={500} height={500}></Stage>
</div>
);
} After: // pages/some-page.js
import React from 'react';
import dynamic from 'next/dynamic';
const MyComponent = dynamic(() => import('../components/MyComponent'), { ssr: false });
export default function Home() {
return (
<div>
<MyComponent></MyComponent>
</div>
);
} // components/MyComponent.js
import React, { useRef } from 'react';
import { Stage, Layer } from 'react-konva'; // notice you can import from react-konva normally
export default function Home() {
const stageRef = useRef();
return (
<div>
<Stage width={500} height={500} ref={stageRef}>
<Layer></Layer>
</Stage>
</div>
);
} 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. |
FWIW, the latest NextJS@11 produces a similar webpack error when importing of an ESM-only npm package. |
@curiousercreative What do you suggest to fix that kind of issue? (typescript with a library that uses type: module in package.json) |
I work around the problem by using this: https://www.npmjs.com/package/next-transpile-modules. Has worked well with several dependencies that otherwise produce the err described above. |
I am running into the same problem using 'globby' for generating website's sitemap. Anyone has any solutions that work? |
Same issue, any updates? |
Same issue here with Next.JS 10.2.3 with Sindre Sohrus's
Has this fix been pushed to the 10.x.x branch ? |
This actually happens intermittently with ReactMarkdown. |
Thanks @Firanolfind! That fixed the following error:
Note that I had to add
|
Anything for remark-gfm @Firanolfind |
@Firanolfind perfect. looks like that fixed my issue.
|
If you need to include a lot of packages in the transpiler list you can generate the list like this: cd node_modules
for d in $(ls); do grep '"type": "module"' "$d"/package.json >/dev/null 2>&1 && echo \"$d\", ; done |
Why is this closed? Couldn't we add a babel post-processor in order to transpile ESM? |
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
|
Thanks @Firanolfind |
@Laci556 Thanks! 🙇🏻♂️ |
No idea if this is a good solution or not, but I followed the errors and built on the solutions by @Firanolfind and @Frosty92 and got const withTM = require("next-transpile-modules")([
"react-markdown",
"remark-gfm",
"micromark-extension-gfm",
"micromark-util-combine-extensions",
"micromark-util-chunked",
"micromark-util-character",
"micromark-util-sanitize-uri",
"micromark-util-encode",
"micromark-util-classify-character",
"micromark-util-resolve-all",
"micromark-factory-space",
"mdast-util-gfm",
"ccount",
"mdast-util-find-and-replace",
"unist-util-visit-parents",
"unist-util-is",
"mdast-util-to-markdown",
"markdown-table",
]);
module.exports = withTM({ webpack5: false }); CC: @ypratham |
For anyone that's struggling with react markdown, version 7 introduced (in August 2021) a breaking change by forcing the use of ESM. |
Thanks @Firanolfind! To keep the types, you can declare the following in a declare module 'react-markdown/react-markdown.min' {
import ReactMarkdown, {
Components,
Options,
uriTransformer,
} from 'react-markdown';
export const Components: Components;
export const Options: Options;
export const uriTransformer: uriTransformer;
export default ReactMarkdown;
} |
@zipang @surferwat did you resolve? Same problem with I did some changes:
Result:
|
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. |
fixed my issue with React-konva |
Hi guys, I am facing this issue in a fresh install, I don't know what I do wrong when building my package. You can repro here: https://github.com/VulcanJS/npm-the-right-way
It might be a mistake in the way I build the package, but I can't tell what's wrong. I am trying to build a solid demo of how to build full-stack packages for Next with modern tooling. Edit: ok #25454 (comment) fixed it, I need to import in 2 steps as described, I cannot dynamically import the npm package. |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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 errorThe 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 withnext/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
andkonva
The text was updated successfully, but these errors were encountered: