reaflow: `require() of ES Module` error in Next.js
I’m submitting a…
[x] Bug report
Current behavior
Unable to use reaflow with the current version of Next.js. I have started a Next.js project using npx create-next-app. After adding reaflow as a dependency and importing it in /pages/index.js, I get the following error in my browser:
Error: require() of ES Module /Users/.../node_modules/p-cancelable/index.js from /Users/.../node_modules/reaflow/dist/index.cjs.js not supported.
Instead change the require of index.js in /Users/.../node_modules/reaflow/dist/index.cjs.js to a dynamic import() which is available in all CommonJS modules.
I tried patching this by overriding the p-cancelable library to it’s previous 2.x version – before its switch to ES modules only in v3 – but this caused another, similar error from another dependency.
Expected behavior
Ideally the library will work as expected on import.
Minimal reproduction of the problem with instructions
Start a new Next.js project (12.0.7) and install reaflow as a dep. Try to import and use { Canvas } in /pages/index.js.
Environment
Libs:
- react version: 17.0.2
- next version: 12.0.7
- reaflow version: ^4.2.15
Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: 16.12.0, 14.18.1
- Platform: macOS
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 9
- Comments: 15 (5 by maintainers)
I was able to run this package by installing a plugin and making two changes in the
next.config.jsfile.I should note though this is my first time using reaflow and I’ve just about run the simplest two node example. So I don’t know if it’ll continue to work with more things added. Plus I don’t mind using experimental flags in the current repository.
This is the actual component I’m running
@amcdnl my two cents…
p-cancellable) are adopting a «ESM-only» strategy. They don’t bother with CJS, they simply set type:module in the package.json and include .js files which are valid ES modules in the package.import()expression. But theimport()expression is async, it returns a Promise, so you can’t use it at the top-level.My suggestion would be to:
(plus the other stuff, but drop
mainandmodule)TypeScript 4.7 has added support for package.json exports: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#package-json-exports-imports-and-self-referencing
This lib not support SSR, so I used dynamic import same @freyandhy but for functions need to import with other way
example import upsertNode
const { upsertNode } = await import(“reaflow”);
If your build fails, try to add types to dynamic import like the code below
@amcdnl will there be a fix for that?
I use Dynamic Import to import the modules. See the example at codesandbox here