Encountered this issue while attempting to integrate ky
into a Next.js project:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /foo/node_modules/ky/index.js
It seems that the cause of this problem is Webpack (or Babel) converting all import
s to require()
s, whereas ky
functions as a pure ES module.
To resolve this issue, I found a workaround by dynamically importing ky
before utilizing it, although this method lacks elegance and efficiency.
const handleFormSubmit = async (event) => {
const ky = (await import("ky")).default;
const response = await ky
.get('http://localhost/api/foo')
.json();
};
Any recommendations for a better approach?