Skip to content

Fix named exports when used#182

Merged
RobinBol merged 2 commits intoathombv:masterfrom
Drenso:fix/named-exports
Apr 16, 2026
Merged

Fix named exports when used#182
RobinBol merged 2 commits intoathombv:masterfrom
Drenso:fix/named-exports

Conversation

@bobvandevijver
Copy link
Copy Markdown
Contributor

When this library is used in an ESM app, the following error can be thrown:

× App Crashed. Stack Trace:
× Named export 'CLUSTER' not found. The requested module 'zigbee-clusters' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'zigbee-clusters';
const { debug, Cluster, CLUSTER } = pkg;

This is due to how the named exports have been defined:

...Clusters,

NodeJS on named imports from CommonJS:

When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static analysis as a convenience for better ecosystem compatibility.

CommonJS modules consist of a module.exports object which can be of any type.

To support this, when importing CommonJS from an ECMAScript module, a namespace wrapper for the CommonJS module is constructed, which always provides a default export key pointing to the CommonJS module.exports value.

In addition, a heuristic static analysis is performed against the source text of the CommonJS module to get a best-effort static list of exports to provide on the namespace from values on module.exports. This is necessary since these namespaces must be constructed prior to the evaluation of the CJS module.

By replacing the problematic spread operator with an object destruction action on the original require to add all imports fully named to the module.exports, we help the heuristic static analysis and prevent issues with named imports in ESM.

@RobinBol RobinBol self-assigned this Apr 16, 2026
@RobinBol RobinBol merged commit 9918740 into athombv:master Apr 16, 2026
0 of 2 checks passed
@bobvandevijver bobvandevijver deleted the fix/named-exports branch April 16, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants