Solved: Invalid Regex Error React Native
It might so happen with you that you try building a react native app (with react-native run-android
or react-native run-ios
), that the metro bundler shuts down abruptly, and doesn’t stay running the way we expect it to. When you run the npm-start
command, it gives you an error that says something along the lines of “Invalid regular expression: ….”
Although the fix is fairly simple and you might be able to find it on forums such as StackOverflow, I just thought it would be a good idea to write a quick article on how you can solve this error.
The cause of the error is this error is a version issue with nodejs. One solution to this problem is to downgrade your node version (from 12.x.x to 10.x.x), but you may not want to do that, just as I didn’t.
So here’s a solution where you don’t have to downgrade your node version. Go into your node_modules directory, and look for metro-config. Next, go into src/defaults, and open the blacklist.js file. Inside this file, you should find a variable names sharedBlacklist. Change the variable code to the one given below, and you should be good to go.
var sharedBlacklist = [
/node_modules[\/\\]react[\/\\]dist[\/\\].*/,
/website\/node_modules\/.*/,
/heapCapture\/bundle\.js/,
/.*\/__tests__\/.*/
];
Running the react-native run-android
/ react-native run-ios
command should work now. Problem solved!
Just remember that you would have to change the code in blacklist.js every time you run the npm install
command.