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.

`@vitejs/plugin-react@1.1.4`: `ReferenceError: React is not defined` when rendering a Dialog-component from `@mui/material`

See original GitHub issue

Describe the bug

When rendering a page containing a Dialog component from @mui/material while using @vitejs/plugin-react version 1.1.4, the app crashes with error message ReferenceError: React is not defined. It seems to be related to the bug fix regarding React.createElement in the latest release.

When switching version to 1.1.3 everything works as expected. Unfortunately, I was not able create an isolated reproduction of the error in StackBlitz. To reproduce, render a MuiDialog in an app using @vitejs/plugin-react version 1.1.4. The issue is only occuring in production builds, not dev environment.

vitejs-plugin-react-error MuiDialog-react-not-defined

Reproduction

StackBlitz repro

System Info

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 1.13 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
    Yarn: 1.22.17 - ~/Dev/naeva/home/node_modules/.bin/yarn
    npm: 6.14.13 - ~/.nvm/versions/node/v14.17.0/bin/npm
  Browsers:
    Chrome: 97.0.4692.71
    Firefox: 85.0.2
    Safari: 15.1
  npmPackages:
    @vitejs/plugin-react: 1.1.4 => 1.1.4 
    vite: ^2.6.4 => 2.7.12

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
yonathan06commented, Jan 24, 2022

Happened to me as well, but only when running vite build without a --mode option When ran vite build --mode staging for example the transformation works fine I believe is something with a different production bundling/transformation logic, but no sure what

My guess it is because of this line: https://github.com/vitejs/vite/blob/e6495f0a52c9bd2cae166934dc965f8955ce035d/packages/plugin-react/src/index.ts#L192

@babel/plugin-transform-react-jsx-development works better with a certain config, and @babel/plugin-transform-react-jsx fails

1reaction
glennbarosencommented, Jan 18, 2022

I’m in way over my head when it comes to this stuff, but my best(and only) bet is that the changes in (ce65c56) broke the way Vite handles our compiled MuiDialog from our component library. Related PR: (#6110)

Since the issue does not happen in the repro above, I am guessing that it is a conflict between babel/vite since we are using babel in our library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught ReferenceError: React is not defined - Stack Overflow
My index.js file looked like this when I was getting Uncaught ReferenceError: React is not defined . import {render} from 'react-dom'; import App...
Read more >
[Solved] ReferenceError: React is not defined
"ReferenceError: React is not defined" is caused by your browser or Javascript linter not knowing where the React keyword is defined.
Read more >
Uncaught ReferenceError: React is not defined
I am trying to make ReactJS work with rails using this tutorial. I am getting this error: Uncaught ReferenceError: React is not defined....
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