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 output with CJS content #701

Open
elrumordelaluz opened this issue Aug 27, 2022 · 7 comments
Open

ESM output with CJS content #701

elrumordelaluz opened this issue Aug 27, 2022 · 7 comments

Comments

@elrumordelaluz
Copy link

elrumordelaluz commented Aug 27, 2022

I am pretty happy using tsup for a couple of packages inside a monorepo (using turbo), but having several problems with a particular one, which fires the error described at #579, in my case, the message is:

Error: Dynamic require of "react" is not supported

Screenshot 2022-08-27 at 17 55 13

The dev and build scripts are the same for all packages, however I am noticing that for this particular one, the .mjs and .js output is pretty similar. I mean that the mjs file has the appearance to be transpiled into cjs, like in the screenshot below, when in the other packages, the same file maintains the import statements and modern structure like the input file.

Screenshot 2022-08-27 at 18 01 22

Thanks in advance!

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
@elrumordelaluz elrumordelaluz changed the title esm output with cjs content ESM output with CJS content Aug 27, 2022
@nitedani
Copy link

Related:
evanw/esbuild#1921
evanw/esbuild#2067

@10Derozan
Copy link

The solution is to external the other cjs packages that required react, like react-dom

@glitch-txs
Copy link

hey could you solve this? I'm using external react and still got the same error

@MatejFacko
Copy link

MatejFacko commented Dec 1, 2023

any update on this guys?

tsup version: 8.0.1

I am using following tsup config:

import { sassPlugin } from 'esbuild-sass-plugin';
import { defineConfig } from 'tsup';

export default defineConfig({
  entry: ['src/index.ts'],
  sourcemap: true,
  clean: true,
  bundle: true,
  dts: true,
  esbuildPlugins: [sassPlugin()],
  splitting: true,
  minify: true,
  format: ['esm', 'cjs'],
  external: ['react', 'react-dom']
});

but in App after npm install I am getting an error (index.js:1 Uncaught Error: Dynamic require of "react" is not supported)

Screenshot 2023-12-01 at 14 30 49

@segevfiner
Copy link
Contributor

@egoist I wonder if you might want to include the workaround that some other bundlers use, but esbuild doesn't, as a plugin in tsup:

import { defineConfig } from 'tsup';

export default defineConfig({
  // *snip*
  banner(ctx) {
    if (ctx.format === "esm") {
      return {
        js: `import { createRequire } from 'module'; const require = createRequire(import.meta.url);`,
      };
    }
  },
});

yuniruyuni added a commit to yuniruyuni/MrDamian that referenced this issue Jun 14, 2024
in tsup configuration, we have ad-hoc banner insertion for require.
this solution is taken from egoist/tsup#701 (comment)
@segevfiner
Copy link
Contributor

Or maybe even as part of the shims option...

@egoist

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

6 participants