Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when importing package in a CommonJS module #232

Open
yongla opened this issue Mar 19, 2025 · 0 comments
Open

Error when importing package in a CommonJS module #232

yongla opened this issue Mar 19, 2025 · 0 comments

Comments

@yongla
Copy link
Contributor

yongla commented Mar 19, 2025

Description

The CommonJS files of the build are treated as ES module files resulting in an error when importing this package in a CommonJS module.

I believe this is due to the way this package has set up its dual CJS/ESM builds and exports. There is a helpful tool called publint that highlights why this is happening for this project: https://publint.dev/@cortex-js/[email protected]

Steps to Reproduce

  1. Have a CommonJS file that imports @cortex-js/compute-engine (v0.28.0)
  2. Run the code that tries to import the package
  3. Notice an error is thrown as shared below

I've also created a basic sandbox showcasing the error: https://codesandbox.io/p/devbox/compute-engine-in-commonjs-mkpsl5

Actual Result

An error like this is thrown at runtime when trying to import the package:

Error [ERR_REQUIRE_ESM]: require() of ES Module /project/sandbox/node_modules/@cortex-js/compute-engine/dist/compute-engine.min.js from /project/sandbox/index.js not supported.
compute-engine.min.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename compute-engine.min.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /project/sandbox/node_modules/@cortex-js/compute-engine/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Expected Result

No error when importing the package in a CommonJS module

Environment

This is the first time I'm using this package, so I'm not sure if this used to work differently in previous version. I've tested this in an isolated node environment running v21.7.3 (the code sandbox above) and in the browser as well within my application.

Compute Engine version 0.28.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant