-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
"React is not defined" when using a custom _app.tsx and .babelrc using React 17 #18096
Comments
I can reproduce the issue without using TypeScript: https://github.com/NBruhno/nextjs-react-not-defined/tree/not-typescript |
I'm getting a similar error but i'm not using TS |
{
"presets": ["next/babel"],
"plugins": [
[
"@babel/plugin-transform-react-jsx",
{
"runtime": "automatic"
}
]
]
} It doesn't seem to work all the time, and _document.tsx has problems too |
my error was caused because I was using |
Just upgraded to next 10 and react 17, having the same issue with a custom _app. I am the one of the guys using typescript, if it can helps somehow. |
I think the problem is when you use a custom .babelrc with React 17. If you just upgrade to next 10, keeping react 16 and custom babelrc, works smoothly. Maybe we need to add something to it? |
This is even funnier, if you import React in the page, it works. :D |
Please calm down with the replies, we are getting notified every time someone posts (hoping for a solution). If you have anything to add which might help narrow down or resolve the issue, which is not already described in the issue initially or in the comments, please do add it. |
Sorry, maybe I was unintentionally malicious. apologies. I just wanted to say that if you import React, it actually works, so this can help understand the problem or any guy occurring in the issue. |
I am also running into this issue when trying to upgrade to React 17, but I'm not using a custom While related to a custom |
I am actually having the opposite of this. I was using Additionally, adding Also this doesn't seem to be directly related to next@10. I only see them problem when upgrading react and react-dom to 17. babel.config.js
|
I just updated using Note that I do not have any custom babel config nor am I using Typescript. I can also confirm that if I |
I went to the trouble of adding back the |
I've just ran in to this issue, the console pointed to an old 'Class' component I had kicking around, importing React as mentioned above or converting to a functional component solved the problem. |
fix: react fragment requires import ? vercel/next.js#18096
Might be related to |
After updating to the latest version of emotionjs (10.1.0),
After updating emotionjs, I added the following to my .babelrc to make it use their new implementation for the JSX runtime:
I thought that maybe the error is from the css optimization during the production build, but turning that off gives the same error. Repo here: https://github.com/NBruhno/nextjs-react-not-defined/tree/emotion-update |
Waiting for the bug to be fixed, you may use Webpack's ProvidePlugin for React package inside a custom
|
@NBruhno I think the new Reported here: emotion-js/emotion#2064 |
As Andarist figured out, it seems like if you reorder the
cc @NBruhno |
Unfortunately @karlhorky, if I change up the order of the presets so
Mind you, this is from my personal project and not the minimal repro, since that doesn't appear to fail. Edit* Actually, having it with
So it does look like a battle of who gets to transform the JSX. |
@Timer One further clue that Andarist found in the other issue is that this error is also happening with other presets:
|
Ok, one last possible way to do it. Not the most beautiful configuration, but this does work - no build errors and no CSS prop
@NBruhno does this work for you? |
@karlhorky I don't see any new issues or failures building it or running it when using '@emotion/core' as the |
If you need a reproduction case, look at UnlyEd/next-right-now#197 My config worked fine, until I tried upgrading to the latest I'll let the branch as-it and wait for a proper solution. The workaround did work. (see UnlyEd/next-right-now@40b18aa) |
@Timer I've done a fair share of debugging this issue and so far it looks like a bug in Babel's config loading logic. It doesn't quite deduplicate plugins in this configuration and according to Babel's docs only the last one defined should be preserved: https://babeljs.io/docs/en/options#pluginpreset-merging I've dug up the PR that has implemented the logic for this flattening but it doesn't contain a test case similar to the situation we are dealing with here. I'm also poking Babel team about this to see if this is intended or a bug (I'm strongly inclined to believe that it's the latter). |
I've written down a lengthy explanation of the problems and the proposed solution here: #18847 |
It appears without using Typescript. |
I'm trying to create the canonical example for Emotion 11 with React 17 and Next.js 10 in #18620 So @Andarist and @Timer, am I understanding correctly when I read the Emotion docs (introduced in emotion-js/emotion#2074) that the following configuration is the current recommendation for using the automatic JSX runtime with Next.js and Emotion?
{
"presets": [
[
"next/babel",
{
"preset-react": {
"runtime": "automatic",
"importSource": "@emotion/react"
}
}
]
],
"plugins": ["@emotion/babel-plugin"]
} Also, I'm assuming that I only need
{
"name": "with-emotion",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"keywords": [],
"author": "Thomas Greco",
"license": "MIT",
"devDependencies": {
- "@emotion/babel-preset-css-prop": "11.0.0"
+ "@emotion/babel-plugin": "11.0.0"
},
"dependencies": {
"@emotion/react": "11.1.1",
"@emotion/styled": "11.0.0",
"next": "latest",
"react": "^17.0.1",
"react-dom": "^17.0.1"
}
} |
Sounds about right - |
…JSX runtime (#18620) ~~‼️ **Blocked** on #18096 Probably resolved here: #18096 (comment) Ref: emotion-js/emotion#2062 Ref: emotion-js/emotion#2041 (comment) Example upgrade here: upleveled/next-js-example-sep-2020@f46407e
This comment has been minimized.
This comment has been minimized.
…JSX runtime (vercel#18620) ~~‼️ **Blocked** on vercel#18096 Probably resolved here: vercel#18096 (comment) Ref: emotion-js/emotion#2062 Ref: emotion-js/emotion#2041 (comment) Example upgrade here: upleveled/next-js-example-sep-2020@f46407e
this worked for me... I'm using next JS |
Thanks, this helped me so much... |
Just reordering
Next v11.1.0 |
@MrJadaml that babelrc config should probably be removed. |
I was getting this after updating to React 17
I fixed it with this commit where I fixed some assumptions that |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
After upgrading to React 17, I can no longer compile my project with a custom
.babelrc
file without importing React in every*.tsx
file. Having a_app.tsx
file seems to be required to reproduce the issue. The error returned is:I'm not sure if TypeScript is related to the issue, but I've simply copy pasted minimal configuration from my own project which is where I experienced the issue.
To Reproduce
I've created the following repo to reproduce the error: https://github.com/NBruhno/nextjs-react-not-defined
yarn
yarn dev
oryarn build
Expected behavior
I could before React 17 run my repo without having to import React in my .tsx files unless explicitly referring to some functions, with my own custom
.babelrc
file.System information
The text was updated successfully, but these errors were encountered: