egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
9.2k stars 219 forks source link

Esm/Cjs issue? #752

Open yovanoc opened 2 years ago

yovanoc commented 2 years ago

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

Fund with Polar

mattkindy commented 1 year ago

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 commented 1 year ago

Appears related to https://github.com/egoist/tsup/issues/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.