metro: ReferenceError: Property 'React' doesn't exist after upgrade react-native to 0.64.0
Do you want to request a feature or report a bug? Report a bug and suggestion to fix it.
What is the current behavior?
I try to upgrade to 0.64.0 from 0.63.4 which include react 17 with the New JSX Transform.
After I remove React import from all my component files.
I got below error when running the app yarn android with yarn start --reset-cache
ReferenceError: Property 'React' doesn't exist
This error is located at:
in App (at renderApplication.js:47)
in RCTView (at View.js:34)
in View (at AppContainer.js:107)
in RCTView (at View.js:34)
in View (at AppContainer.js:134)
in AppContainer (at renderApplication.js:40), js engine: hermes
ReferenceError: Property 'React' doesn't exist
This error is located at:
in App (at renderApplication.js:47)
in RCTView (at View.js:34)
in View (at AppContainer.js:107)
in RCTView (at View.js:34)
in View (at AppContainer.js:134)
in AppContainer (at renderApplication.js:40), js engine: hermes
If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.
- initialize fresh react-native 0.63.4
npx react-native init AwesomeTSProject --template react-native-template-typescript - use upgrade-helper to upgrade to react-native 0.64.0
- remove any
import React from 'react';from component file. - error will show
ReferenceError: Property 'React' doesn't exist
What is the expected behavior? As from React blog about New JSX Transform, it needs additional config in babel.config.js
// If you're using @babel/plugin-transform-react-jsx
{
"plugins": [
["@babel/plugin-transform-react-jsx", {
"runtime": "automatic"
}]
]
}
- After I add this into my babel.config.js and
yarn start --reset-cacheandyarn androidthe error was gone.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
- metro-react-native-babel-preset 0.64.0
- node 14.16.0
- yarn 1.22.5
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
};
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 10
- Comments: 16
Commits related to this issue
- Enable react 17 new jsx transform See https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#runtime Fixes #646 — committed to danilobuerger/metro by danilobuerger 4 years ago
- Enable react 17 new jsx transform (#848) Summary: See https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#runtime Fixes https://github.com/facebook/metro/issues/646 Pull Request resolved: h... — committed to Titozzz/metro by danilobuerger 4 years ago
Because,
metro-react-native-babel-presetincludes@babel/plugin-transform-react-jsxby default. If you add it toplugins, it would be duplicated. https://github.com/facebook/metro/blob/6cb3ec88f707b1beb78462be11195b5d496e3aad/packages/metro-react-native-babel-preset/src/configs/main.js#L59-L61But set
useTransformReactJSXExperimental: trueis not enough. To use new JSX transformation, you have to passruntime: 'automatic'to@babel/plugin-transform-react-jsxtoo (runtimeisclassicby default). https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#manual-babel-setupSo you might want to change your
babel.config.jsto:Don’t forget to run
yarn start --reset-cacheafter making changes tobabel.config.js@jmfigueroa Your
presetsis in wrong format. If you want to pass options to a preset, you have to use the array syntax with:not
it makes babel think that you are using 2 presets
module:metro-react-native-babel-preset{ 'useTransformReactJSXExperimental': true }Thanks @moonlight8978. Your suggestion of adding
useTransformReactJSXExperimental: trueto the metro preset configs worked like a charm.yarn start --reset-cache solved my issues with typescript
Hello, I followed this comment and c&p the given
babel.config.jsinto my project but I’m still getting theReferenceError: Can't find variable: Reacterror. Do I need to reload babel or something? I already tried to clear npm cache without success. Here’s my config :I tried having the
@babel/plugin-transform-react-jsdependency directly into my package.json but it didn’t work. When creating my react-native project withnpx react-native initI directly updated@babel/coreand@babel/runtimeto their latest version. Maybe it has introcude a conflict? I would greatly appreciate some help because it seems to be working just fine for lots of people I don’t understand what I did wrongHi, @moonlight8978. 👋
Thanks for the message. I checked
babel-preset-expoand it looks like it extendsmetro-react-native-babel-presetbut adds a couple of Expo-specific features. Despite having ejected to the bare workflow, I’m still using@expo/vector-icons. The problem, I think, is that it’s pinning an old version ofmetro-react-native-babel-presetbut Expo SDK 41 is due out at the end of the month so hopefully they’ll update it.Regardless, I tried removing
babel-preset-expoand had trouble with loading icon fonts (they mention here how the preset handles that). What I ended up doing is this:I don’t have that much experience with Babel so I’m not sure how acceptable this is but I can say that by doing this, I no longer get the Can’t find variable: React error and I have no problems with the icon fonts, etc. Maybe, once
babel-preset-expois updated to support the New JSX Transform, I can remove one of the two. Thanks again for your message.Having the same issue now with SDK53 …
I am using react-native with typescript. I am still facing this issue. Here’s my
babel.config.js:I also tried installing
@babel/plugin-transform-react-jsxand changedjsxtoreact-jsxintsconfig.jsonbut it didn’t help. I would appreciate any help on this.Solution from https://github.com/facebook/metro/issues/646#issuecomment-799174473 is working on development for me, but when I make production build it doesn’t work with error “Can’t find variable: React”