question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing react/jsx-runtime breaks dev builds

See original GitHub issue

Describe the bug

When importing react/jsx-runtime, dev build breaks with the following client error:

Uncaught ReferenceError: module is not defined
    at jsx-runtime.js:6

In my more complex setup I am consuming a local prebuilt library and get a more weird error message

Uncaught ReferenceError: module is not defined
    at null:6

I’ve found out that setting optimizeDeps.include: ['react/jsx-runtime'] fixes this issue and in fact 'react/jsx-dev-runtime' works because @vitejs/plugin-react adds it by default. Does it make sense to also include 'react/jsx-runtime' there?

Reproduction

StackBlitz

System Info

System:
    OS: macOS 12.0.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 69.96 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.volta/tools/image/node/16.13.0/bin/node
    Yarn: 1.18.0 - ~/.volta/tools/image/yarn/1.18.0/bin/yarn
    npm: 8.1.0 - ~/.volta/tools/image/node/16.13.0/bin/npm
    Watchman: 2021.06.07.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 96.0.4664.110
    Edge: 96.0.1054.62
    Firefox: 95.0
    Safari: 15.1
  npmPackages:
    @vitejs/plugin-react: ~1.1.3 => 1.1.3 
    vite: ~2.7.4 => 2.7.4

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
ecarreracommented, Mar 24, 2022

Change the configuration of your @vitejs/plugin-react instance to the classic mode of the jsxRuntime

vite.config.js

import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [
    react({
      jsxRuntime: 'classic',
    }),
  ]
});

Reference

Opting out of the automatic JSX runtime

By default, the plugin uses the automatic JSX runtime. However, if you encounter any issues, you may opt out using the jsxRuntime option.

react({ jsxRuntime: ‘classic’ })

7reactions
arm1ncommented, Mar 10, 2022

For me this proposed solution works as a temporary hotfix:

resolve: {
  alias: {
    'react/jsx-runtime': 'react/jsx-runtime.js',
  },
},

Will your PR covers this issue @aleclarson? Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing the New JSX Transform – React Blog
Instead of transforming JSX to React.createElement , the new JSX transform automatically imports special functions from those new entry points ...
Read more >
can't resolve react/jsx-dev-runtime' after changing "type":"
BREAKING CHANGE: The request 'react/jsx-dev-runtime' failed to resolve only because it was resolved as fully specified (probably because the ...
Read more >
API - ESBuild
There are two main API calls in esbuild's API: transform and build. ... JSX dev; JSX factory; JSX fragment; JSX import source; JSX...
Read more >
cannot find module '@emotion/react/jsx-runtime' or its ...
If you're still getting the "Cannot find module 'react/jsx-runtime' or its corresponding type declarations" error, open your package.json file and make sure it ......
Read more >
babel/plugin-transform-react-jsx
Automatic runtime is a feature added in v7.9.0 . With this runtime enabled, the functions that JSX compiles to will be imported automatically....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found