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

Esm/Cjs issue? #752

Open
yovanoc opened this issue Oct 24, 2022 · 2 comments
Open

Esm/Cjs issue? #752

yovanoc opened this issue Oct 24, 2022 · 2 comments

Comments

@yovanoc
Copy link

yovanoc commented Oct 24, 2022

First of all my entire codebase is in TS. I have an api which is in CJS. It imports a common library in my Monorepo that is in originally in ESM. (type: module in package.json) but this is build with tsup in js files in esm and cjs. BUT the problem is that my Time.cjs file import another file (Random.js) in my common lib and I think tsup don't correctly link this. It keeps the Random.js instead of Random.cjs. Or maybe am I missing something? Thanks in advance for the help.

Internal error occurred during message handling. Please check your implementation. Error [ERR_REQUIRE_ESM]: require() of ES Module /app/packages/common/dist/Random.js from /app/packages/common/dist/Time.cjs not supported.
Instead change the require of Random.js in /app/packages/common/dist/Time.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/app/packages/common/dist/Time.cjs:1:88)
    at /app/apps/api/dist/types/subscriptions/index.js:1:748

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@mattkindy
Copy link

mattkindy commented Nov 16, 2022

I have a similar scenario where I'm including a dependency i18next which depends on @babel/runtime.

In the produced index.cjs file, we see an attempt to require the ESM for these files:

// node_modules/i18next/dist/esm/i18next.js
var import_typeof = __toESM(require("@babel/runtime/helpers/esm/typeof"), 1);
var import_classCallCheck = __toESM(require("@babel/runtime/helpers/esm/classCallCheck"), 1);
var import_createClass = __toESM(require("@babel/runtime/helpers/esm/createClass"), 1);
var import_assertThisInitialized = __toESM(require("@babel/runtime/helpers/esm/assertThisInitialized"), 1);
var import_inherits = __toESM(require("@babel/runtime/helpers/esm/inherits"), 1);
var import_possibleConstructorReturn = __toESM(require("@babel/runtime/helpers/esm/possibleConstructorReturn"), 1);
var import_getPrototypeOf = __toESM(require("@babel/runtime/helpers/esm/getPrototypeOf"), 1);
var import_defineProperty = __toESM(require("@babel/runtime/helpers/esm/defineProperty"), 1);
var import_toArray = __toESM(require("@babel/runtime/helpers/esm/toArray"), 1);

This results in an error:

var import_typeof = __toESM(require("@babel/runtime/helpers/esm/typeof"), 1);
                            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/@babel/runtime/helpers/esm/typeof.js from node_modules/utils-lib/dist/index.cjs not supported.
Instead change the require of typeof.js in node_modules/utils-lib/dist/index.cjs to a dynamic import() which is available in all CommonJS modules.

@mattkindy
Copy link

mattkindy commented Nov 16, 2022

Appears related to #628

Essentially, this is a problem caused by transitive dependencies that may not be in the same format as the bundle. I suppose this is the fault of the initial dependency in some sense. To solve my problem, I ended up adding @babel/runtime to my list of noExternals manually so it would be properly inlined.

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

2 participants