Node (16/next) imports CommonJS instead of ESM due to missing type
and exports
in package.json
#3435
Closed
1 of 2 tasks
Labels
Type: Bug
The issue or pullrequest is related to a bug
What’s the bug you are facing?
There have been several user discussions and bug reports related to
prosemirror-model
failing with some form oflooks like multiple versions of prosemirror-model were loaded
, such as:Some of these are due to multiple versions getting loaded, e.g. CommonJS and ES Modules, at the same time and then causing runtime errors.
We recently ran into this with our codebase (all TypeScript/ESM) and build system (Rush/Heft) and tracked it down to the
package.json
files not having"type": "module"
and the newerexports
field set.The TypeScript documentation regarding ECMAScript Modules in Node.js has a good explanation and resolution.
For example,
tiptap-core
works great by changing the file extension for CommonJS to.cjs
(and ESM to.mjs
for consistency and explicitness) and modifyingpackage.json
, so that it provides the new syntax and fallbacks for older versions of Node and other build systems:but without we get a
RangeError: Can not convert (something) to a Fragment (looks like multiple versions of prosemirror-model were loaded)
when different code paths load a mix of CJS and ESM (our library, y-prosemirror, tiptap, etc.).Note: If I'm reading Dangers of exporting both CJS and ESM in Dual package hazard correctly, it is still possible to run into the same issue if there is shared state, but these changes should at least clean up the code paths in many cases.
Which browser was this experienced in? Are any special extensions installed?
In Node, not the browser.
How can we reproduce the bug on our side?
npm init
"type": "module",
topackage.json
.index.mjs
and add the following code to it:node index.mjs
Can you provide a CodeSandbox?
No response
What did you expect to happen?
The expectation is that an
import
from an ESM project uses a single ESM instance of TipTap, so that these errors don't happen. Also that CommonJS loads CJS.Anything to add? (optional)
I have a PR ready to go that patches all the
package.json
files for the repo, and I will submit it shortly.Did you update your dependencies?
Are you sponsoring us?
The text was updated successfully, but these errors were encountered: