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

Cannot find module .../jsx-runtime #2468

Closed
6 tasks done
unional opened this issue Dec 8, 2022 · 6 comments
Closed
6 tasks done

Cannot find module .../jsx-runtime #2468

unional opened this issue Dec 8, 2022 · 6 comments

Comments

@unional
Copy link

unional commented Dec 8, 2022

Describe the bug

I have a library that depends on another library. Both using vite to build with react@17, TypeScript, pnpm, and in ESM.

The build works fine, but vitest failed with:

Error: Cannot find module 'D:\code\justland\just-web-react\libraries\react\node_modules\react\jsx-runtime' imported from D:\code\justland\just-web-react\libraries\react\dist\just-web-react.js
Did you mean to import react/node_modules/.pnpm/react@17.0.2/node_modules/react/jsx-runtime.js?

Reproduction

Since it involves one library referencing another, it will take some effort to come up with a minimal repro.

For the time being, here is the code:

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
    Memory: 31.94 GB / 63.92 GB
  Binaries:
    Node: 14.20.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.14.17 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.819.0), Chromium (108.0.1462.42)
    Internet Explorer: 11.0.22621.1

Used Package Manager

pnpm

Validations

@unional
Copy link
Author

unional commented Dec 8, 2022

I can confirm this is an vitest issue. I have two other repos that consume the said package, the one with vitest has the same issue, the other one using jest does not.

@sheremet-va
Copy link
Member

Your files need to be processed by Vitest (they are externalised in your case). Add this to your vite.config.ts:

export default {
  test: {
    deps: {
      inline: [/@just-web/, /just-web-react/]
    },
  },
}

I was also able to fix the issue by adding .js extension to import, since jsx-runtime is not specified in react's exports field (otherwise, it's required by ESM spec). This would actually be faster than inlining dependency.

@unional
Copy link
Author

unional commented Dec 8, 2022

awesome! thanks for the quick response. 🌷

@unional unional closed this as completed Dec 8, 2022
@sheremet-va
Copy link
Member

I was also able to patch react package with these exports to make this work:

{
  "exports": {
    ".": {
      "default": "./index.js"
    },
    "./jsx-runtime": {
      "default": "./jsx-runtime.js"
    },
    "./jsx-dev-runtime": {
      "default": "./jsx-dev-runtime.js"
    }
  }
}

This all is required, because if you delegate executing to Node (which is enabled for your package here), then it kicks it's own resolution mechanism instead of Vite's, and it doesn't support importing react/jsx-runtime (but does support require('react/jsx-runtime'))

This is unfortunately sad ESM<->CJS reality that we have to live in.

@unional
Copy link
Author

unional commented Dec 8, 2022

Yeah, I'm using React 17 which does not have the exports fields at all.

Maybe I can file a PR to React to fix that in 17 (17.0.3)

@geekact
Copy link

geekact commented Dec 19, 2022

Yeah, I'm using React 17 which does not have the exports fields at all.

Maybe I can file a PR to React to fix that in 17 (17.0.3)

Have a look at this issue and it'll never be fixed for react 16 and 17
facebook/react#20235

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

No branches or pull requests

3 participants