-
Notifications
You must be signed in to change notification settings - Fork 10.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 17 new JSX Transform: ReferenceError: React is not defined #28657
Comments
This has been disabled by default because "the ecosystem hasn't adapted yet": #27615 |
@nikoladev At least offer a flag!? |
@KyleAMathews you can't go around selling that Gatsby has the new JSX Transform and actually having this stated in the React docs and then disable without any warning. Gatsby is not a personal blog framework. Big companies depend on it |
I'm also just a user of Gatsby 🤷♀️ You could open up a PR with your proposed flag. |
@nikoladev ah alright. I already commented all related issues with the solution, but I don't have much time to open a PR and add tests. This is a better job for someone who understands the project. I just left the user side solution |
@Bartmr have you enabled react-refresh? The biggest architecture issue we're having right now with having a flag for it, plugins like mdx, emotion still need to have the proper |
@wardpeet i do have it enabled, but I do not use any library that needs to mess around with the jsx (like emotion or mdx). Problably should try disabling it. But even if it doesn't work now, it should work in the future since fast refresh is what's planned to be the default. |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
This is still an issue, please don't close. I ran into this today. |
Thanks @Bartmr for the work around. It worked perfectly! 😊 In exports.onCreateBabelConfig = ({ actions }) => {
actions.setBabelPlugin({
name: '@babel/plugin-transform-react-jsx',
options: {
runtime: 'automatic',
},
});
}; |
I'm getting this error, too, and then when I put in the code from the latest @Dan503 comment, and then run
However, when I execute
So Edit I've upgraded to Gatsby 2.31.1 and @babel/core and @babel/runtime 7.12.13, plus react and react-dom 17.0.1 Does the fact that I'm writing TypeScript instead of JavaScript have any effect on this? Does Gatsby only apply the babel jsx-runtime plugin to JSX and not TSX files? |
I'm using Typescript in my project and haven't faced that issue. @kylegoetz It might be that your node_modules folder is corrupt. Try deleting node_modules and reinstalling. It might fix the issue for you. |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Will this be enabled in Gatsby v3? |
It is very strage, but with the all the freshest packages including core ones:
Still seeing: But the expected behavior is the possibility to avoid importing React in each file due to new JSX-transform feature, that was introduced pretty long ago. I expected that with upgrade to Gatsby 3 it will be packed up inside automatically. |
@steverandy @dvakatsiienko Gatsby 3 supports the new React JSX transform but it is disabled by default (#27615), you need to enable it in your {
"presets": [
[
"babel-preset-gatsby",
{
"reactRuntime": "automatic"
}
]
]
} More info: https://www.gatsbyjs.com/docs/reference/release-notes/v3.0/#react-17 |
@mohatt Thank you very much! |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
@mohatt your suggestion does not seem to work for me I still end up with this ERROR #98123 WEBPACK
Generating development JavaScript bundle failed
/home/ibqn/devel/gatsby3/src/pages/index.js
1:25 error 'React' must be in scope when using JSX react/react-in-jsx-scope
✖ 1 problem (1 error, 0 warnings)
failed Building development bundle - 13.352s
ERROR in
/home/ibqn/devel/gatsby3/src/pages/index.js
1:25 error 'React' must be in scope when using JSX react/react-in-jsx-scope
✖ 1 problem (1 error, 0 warnings)
webpack compiled with 1 error
|
Same for me. Adding |
@ibqn @codejet those are just eslint errors due to the rule {
// ...
"rules": {
// ...
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off"
}
} @codejet btw you don't have to install See: |
Thanks @mohatt! That seems to work. Only small issue that I still have now is that using shorthand fragments ( |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
This is great information, we should adjust our internal eslint config based on this setting |
Working off @mohatt 's solution, adding this to
|
Just wanted to add that I'm having exactly the same issue--new JSX transform works fine until I try using |
I haven't solved this yet, @iansjk. so, i'm still using |
I managed to solve the exports.onCreateBabelConfig = ({ actions }) => {
actions.setBabelPreset({
name: 'babel-preset-gatsby',
options: {
reactRuntime: 'automatic',
},
})
} However I can't get Seeing this in dev tools Which indicates the the I'm using the Setup "react": "^17.0.2",
"gatsby": "3.7.2",
"typescript": "^4.3.3",
"gatsby-plugin-emotion": "^6.12.0",
"@emotion/react": "^11.4.1",
"@emotion/styled": "^11.3.0",
"@emotion/babel-plugin": "^11.3.0",
"@emotion/eslint-plugin": "^11.2.0", I've tried the EDIT I finally managed to solve this, by removing {
"presets": [
[
"@babel/preset-react",
{ "runtime": "automatic", "importSource": "@emotion/react" }
]
],
"plugins": ["@emotion"]
} |
Description
Gatsby 2.28.1 will throw a
ReferenceError: React is not defined
if React is not imported on top of the file, but the new JSX Transform (built-in in Gatsby since 2.24) supports files with JSX without a React Import.Steps to reproduce
I couldn't reproduce this in CodeSandbox but you should easily reproduce this by rendering a component that does not import React.
Expected result
Should not throw the error and allow components without a React import to work normally
Actual result
What happened.
It happens in both development and build mode
The output JS file does not have any React reference:
Environment
It happens in both development and build mode
It also happens in NodeJS 12
System:
OS: Linux 4.15 Ubuntu 18.04.5 LTS (Bionic Beaver)
CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
Shell: 4.4.20 - /bin/bash
Binaries:
Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
npm: 6.9.0 - ~/.nvm/versions/node/v10.16.0/bin/npm
Languages:
Python: 2.7.17 - /usr/bin/python
Browsers:
Chrome: 87.0.4280.88
npmPackages:
gatsby: ^2.28.1 => 2.28.1
gatsby-image: ^2.7.0 => 2.7.0
gatsby-plugin-manifest: ^2.8.0 => 2.8.0
gatsby-plugin-offline: ^3.6.0 => 3.6.0
gatsby-plugin-react-helmet: ^3.6.0 => 3.6.0
gatsby-plugin-robots-txt: ^1.5.3 => 1.5.3
gatsby-plugin-sass: ^2.7.0 => 2.7.0
gatsby-plugin-sharp: ^2.10.1 => 2.10.1
gatsby-plugin-sitemap: ^2.8.0 => 2.8.0
gatsby-plugin-svgr: 2.0.2 => 2.0.2
gatsby-plugin-webpack-bundle-analyser-v2: ^1.1.18 => 1.1.18
gatsby-source-filesystem: ^2.7.0 => 2.7.0
gatsby-transformer-sharp: ^2.8.0 => 2.8.0
npmGlobalPackages:
gatsby-cli: 2.7.49
Possible solution:
I was able to go around this bug by adding this to
gatsby-node.js
:The text was updated successfully, but these errors were encountered: