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

Bug: react 16 and 17 not working in ESM env with TypeScript #25857

Closed
unional opened this issue Dec 8, 2022 · 7 comments
Closed

Bug: react 16 and 17 not working in ESM env with TypeScript #25857

unional opened this issue Dec 8, 2022 · 7 comments

Comments

@unional
Copy link

unional commented Dec 8, 2022

React version: 16 and 17

Reference: vitest-dev/vitest#2468

Steps To Reproduce

  1. using react 16/17 in a ESM project (and e.g. with vite)
  2. using react-jsx typescript setting

There will be error of "Cannot find module" for react/jsx-runtime

The current behavior

Not working, needs workaround

The expected behavior

Should work.

I can help with PR to add the exports field to React 16/17.

@unional unional added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Dec 8, 2022
@idler8
Copy link

idler8 commented Mar 15, 2023

look it
reactjs/react.dev#5659

@eps1lon
Copy link
Collaborator

eps1lon commented Mar 25, 2023

It doesn't look like this bug report has enough info for one of us to reproduce it.

Please provide a CodeSandbox (https://react.new), or a link to a repository on GitHub.

Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve

@eps1lon eps1lon added Resolution: Needs More Information and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Mar 25, 2023
@unional
Copy link
Author

unional commented Mar 29, 2023

The support of ESM was added in 18, it is not there in 16 and 17.

@unional
Copy link
Author

unional commented Jul 19, 2023

Here is a repro: https://github.com/cyberuni/react-16-vite-ts-issue

pnpm i
pnpm vitest

@unional
Copy link
Author

unional commented Jul 19, 2023

Adding this to packages/react/package.json will fix the issue:

  "exports": {
    ".": {
      "default": "./index.js"
    },
    "./package.json": "./package.json",
    "./jsx-runtime": "./jsx-runtime.js",
    "./jsx-dev-runtime": "./jsx-dev-runtime.js",
    "./src/*": "./src/*"
  },

However I can't modify the code and commit because when trying to do a yarn install on v16.14.0,
I got:

Response code 404 (Not Found) for https://github.com/electron/electron/releases/download/v9.1.0/electron-v9.1.0-darwin-arm64.zip

@unional
Copy link
Author

unional commented Jul 24, 2023

dup of #20235

with some workarounds:
#20235 (comment)
#20235 (comment)

@unional unional closed this as completed Jul 24, 2023
@unional
Copy link
Author

unional commented Aug 9, 2023

Note that the workaround for webpack does not work for vitest if the issue comes from a dependency using react-jsx option.

This is because vite uses esbuild to compile individual dependency files, and there is no way to use plugin to perform aliasing.

The workaround in vitest is using dependency inlining:

{
  test: {
    deps: {
      inline: ['dep-package']
    }
  }
}

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

No branches or pull requests

3 participants