-
-
Notifications
You must be signed in to change notification settings - Fork 37
Description
Describe the bug
When using xmlbuilder2 v4.0.0 in a CommonJS Node.js project, the application fails at runtime with an ERR_REQUIRE_ESM error. This occurs because the dependency @oozcitak/util (v9.0.4) is published as a pure ES Module ("type": "module"), but xmlbuilder2 attempts to require() it from CommonJS code.
Error Message
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/xmlbuilder2/node_modules/@oozcitak/util/lib/index.js from /path/to/node_modules/xmlbuilder2/lib/builder/XMLBuilderImpl.js not supported.
Instead change the require of index.js in /path/to/node_modules/xmlbuilder2/lib/builder/XMLBuilderImpl.js to a dynamic import() which is available in all CommonJS modules.
To Reproduce
package.json:
{
"dependencies": {
"xmlbuilder2": "^4.0.0"
}
}app.js:
const { create } = require('xmlbuilder2');
const doc = create({ version: '1.0' })
.ele('root')
.ele('element').txt('value')
.end({ prettyPrint: true });
console.log(doc);Run:
node app.jsResult: ERR_REQUIRE_ESM error
Expected behavior
The library should work seamlessly in CommonJS Node.js environments without throwing ES Module
compatibility errors. When requiring and using xmlbuilder2 in a CommonJS project, it should function
as documented.
Potential Solutions
We understand this is a challenging compatibility issue. Here are some approaches that might help
resolve it:
- Using dynamic
import()instead ofrequire()for ES Module dependencies - Bundling
@oozcitak/utilinto xmlbuilder2's output distribution - Pinning
@oozcitak/utilto a CommonJS-compatible version - Publishing xmlbuilder2 as a dual CommonJS/ESM package (similar to [this approach](https://stackoverf
low.com/questions/74937600/how-to-support-es-modules-and-commonjs-modules-at-the-same-time))
Any guidance on recommended workarounds would also be greatly appreciated!
Additional Requests
- Documentation highlighting any ES Module requirements or migration paths for CommonJS users
- Release notes indicating when dependencies shift to ES Modules (as this can be a breaking change for
CommonJS consumers)
Version:
- Node.js version: 22.14.0
- xmlbuilder2 version: 4.0.0
Additional context
Similar to #201 - this appears to be the same underlying ES Module compatibility problem affecting different dependency versions.
Are there any recommended workarounds for CommonJS projects until this is resolved? We'd appreciate any guidance on how to proceed.