react-native: [0.56.0][iOS][Android] Can't find variable: require
- Review the documentation: https://facebook.github.io/react-native
- Search for existing issues: https://github.com/facebook/react-native/issues
- Use the latest React Native release: https://github.com/facebook/react-native/releases
Environment
`react-native info` output
React Native Environment Info:
System:
OS: macOS 10.14
CPU: x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Memory: 3.28 GB / 32.00 GB
Shell: 5.5.1 - /usr/local/bin/zsh
Binaries:
Node: 10.9.0 - ~/.nvm/versions/node/v10.9.0/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.2.0 - ~/.nvm/versions/node/v10.9.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0
Android SDK:
Build Tools: 23.0.1, 23.0.3, 25.0.1, 25.0.3, 26.0.1, 26.0.2, 27.0.3, 28.0.2
API Levels: 23, 24, 25, 26, 28
IDEs:
Android Studio: 3.1 AI-173.4907809
Xcode: 10.0/10L232m - /usr/bin/xcodebuild
npmPackages:
react: ^16.4.2 => 16.4.2
react-native: 0.56.0 => 0.56.0
Description
- Application was upgraded to 0.56.0 from 0.55.4
- Had a lot of issues with getting set up, mostly stemming from jest tests
- Fixed them, now all
babelis pointing to7.0.0-beta.47by usingyarnresolutions
When building for development or release, I get a can't find variable: require error. This happens on both iOS and Android, and seems to be in code that I have no control over. Here is the output on iOS:

Android doesn’t make it far enough to get a Redbox - it throws before it gets to that point, but I same the same error thrown using logcat. The error on Android is com.facebook.react.devsupport.JSException: Can't find variable: require.
I have seen #19827 (and its various children), but all of them seem to imply that only production is broken, not development. And none of the errors included have anything to do with require.
Based on comments in other threads: I am not using generators. I am using async/await.
I have tried removing node_modules, clearing the watchman cache, clearing the yarn cache, clearing the build folder in XCode, clearing the Android build artifacts.
At first I thought that this issue was due to Mojave, but the builds produced by my CI platform are also broken, and they are running in Xcode 9.4.1 and macOS Sierra/High Sierra, or Linux for Android builds.
Reproducible Demo
I have no idea how to reproduce this at this stage. Still investigating.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 20 (3 by maintainers)
had this error and fixed it by removing
"@babel/preset-env"from the babel presets, then runreact-native start --reset-cacheNothing worked for me until I did reset the transform cache:
Now it works with package.json
and babel.config.js
Looks like root cause was transform caching failure…?
It looks like you are using an older version of React Native. Please update to the latest release, v0.57 and verify if the issue still exists.
The “⏪Old Version” label will be removed automatically once you edit your original post with the results of running
react-native infoon a project using the latest release.OMG. It worked for me as well. I spent 5 hours straight into this error, and I was trying many different .babelrc setups and none of them worked only because the packager cached the very first wrong one…
@m-vdb try to remove
@babel/react, you don’t need it as you havemodule:metro-react-native-babel-presetand maybe try to move@babel/syntax-class-propertiesafter@babel/proposal-decoratorsand as you havelegacy: trueto proposal-decorators you have to setloose: falseto@babel/proposal-class-propertiesand maybe try to remove the preset env.found that was caused by corejs option
@babel/plugin-transform-runtime, I replaced it by@babel/polyfill, here is my files: .babelrcpackage.json
and I added in my app’s code
import '@babel/polyfill'hope this helps
Ok it seems that it’s babel config related, so mostly a single project issue or Metro one. Atm when installing a new project it should be created with Metro 0.48+ which should fix these issues. We’ll also do a new 0.57.3 soon to make sure that also “upgrading” uses it.
Thank you! Finally my app loads again after many many wasted hours! 🎉
That’s what seemed to do it for me.
Plus I also originally had this in my .babelrc file:
The Typescript resource that RN linked to in RN 0.56 said to add those @babel presets/plugins…but in the end, but I guess that’s only valid for React web, not RN, so I now just have this and it’s working (but only after I ran that rest of the transform cache command above):
So, it seems to me that the solution is simply to reset the cache of the bundler, and is not an actual bug of the
react-nativecodebase.Can this be closed then?
thanks a lot @takion 🙏all your recommendations together fixed it! (then I stumbled upon an other error, https://github.com/facebook/react-native/issues/20588, and I managed to fixed that too, I think).
key takeaway: cleanup babel config and rely on
module:metro-react-native-babel-presetplugin. Documentation is scarce so I looked into the code of the plugin to know which plugin I needed to include or not.