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

CJS dynamic import default ESM package cause Error [ERR_REQUIRE_ESM]: require() #9995

Open
AlstonChan opened this issue Nov 1, 2024 · 0 comments

Comments

@AlstonChan
Copy link

🐛 bug report

When using importing ESM package in a CJS project using dynamic import, it will throw the error of unable to require ESM file. This only happens when dynamic importing package that uses default export, while named export works fine. When inspecting the built file, it seems that the await import has been transformed to await require() for both named and default exports, but somehow breaks when the package uses default export.

🎛 Configuration (.babelrc, package.json, cli command)

package.json

{
  "name": "test-parcel",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "parcel build index.cjs --target default --no-scope-hoist --no-optimize  --log-level verbose",
    "start": "node index.js"
  },
  "engines": {
    "node": ">=20.0.0"
  },
  "targets": {
    "default": {
      "context": "node",
      "distDir": "./"
    }
  },
  "devDependencies": {
    "@types/node": "^22.8.6",
    "parcel": "^2.12.0"
  },
  "dependencies": {
    "mathjs": "^13.2.0",
    "ora": "^8.1.0"
  }
}

🤔 Expected Behavior

dynamic import of package that uses default export should work just like named export.

😯 Current Behavior

dynamic import of package that uses default export throws error

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\test\test-parcel\node_modules\ora\index.js from D:\test\test-parcel\index.js not supported.
Instead change the require of D:\test\test-parcel\node_modules\ora\index.js in D:\test\test-parcel\index.js to a dynamic import() which is available in all CommonJS modules.
    at newRequire (D:\test\test-parcel\index.js:58:18)
    at newRequire (D:\test\test-parcel\index.js:45:18)
    at localRequire (D:\test\test-parcel\index.js:84:35)
    at D:\test\test-parcel\index.js:152:36
    at 3ZIAi.cf029259061c951f (D:\test\test-parcel\index.js:157:3)
    at newRequire (D:\test\test-parcel\index.js:71:24) {
  code: 'ERR_REQUIRE_ESM'
}

💁 Possible Solution

i dont know.

🔦 Context

Trying to import package from ESM to CJS, but fails

💻 Code Sample

index.cjs

(async () => {
  //   const { create } = await import("mathjs");
  //   console.log(create);
  const { default: ora } = await import("ora");
  console.log(ora);
})();
  • mathjs package is an ESM package that uses named export. If you comment out ora import line and console log, you should see that the compile file can run successful with a function logged in console.
  • ora package is also an ESM package but uses default export. If you comment out mathjs import line and console log, you will see the error being thrown.

🌍 Your Environment

Software Version(s)
Parcel 2.12.0
Node 20.18.0
npm/Yarn 10.9.0
Operating System Windows 10.0.22631 Build 22631
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant