使用Expo-CLI启动React Native项目时遇正则表达式错误求助
Hey there! That regex error you're hitting is super common when using older Expo/Metro versions with Node.js 12 or newer—let's work through how to fix this.
What's causing this?
Node.js 12 tightened up its regex syntax checks, and the older version of Metro Bundler (the tool Expo uses to bundle your app) has a malformed regex in its blacklist config. The problematic bit is node_modules[\\\]react[\\\]dist[\\\].*—those triple backslashes break the regex structure, triggering the "unterminated character class" error.
Solutions to try:
1. Upgrade your Expo project (Recommended)
This is the best long-term fix, as it brings your project up to date with dependencies that play nice with newer Node versions:
- Open your terminal, navigate to your project folder:
cd D:\Final Year Project\practice\rn-test-app - Run the Expo upgrade command:
expo upgrade - Follow the prompts to update your project to the latest compatible Expo SDK version. Once done, fire up
npm startagain.
2. Manually patch the Metro config (Temporary quick fix)
If you can't upgrade right now, you can fix the problematic regex directly (note: this change will get overwritten if you reinstall node_modules later):
- Head to
node_modules/metro-config/src/defaults/blacklist.jsin your project - Find the line with the regex that includes
node_modules[\\\]react[\\\]dist[\\\].* - Replace the triple backslashes (
\\\) with double backslashes (\\) so that section becomes:node_modules[\\/]react[\\/]dist[\\/].* - Save the file, then restart your Expo dev server with
npm start
3. Downgrade Node.js to version 10.x
If neither of the above works, you can use a Node version manager (like nvm for Windows/macOS/Linux) to switch to Node 10, which doesn't have this strict regex validation:
- Install nvm if you haven't already
- Run these commands:
nvm install 10 nvm use 10 - Restart your terminal, then run
npm startagain
Your error log for reference:
EXPO CLI ERROR
PS D:\Final Year Project\practice\rn-test-app> npm start
@ start D:\Final Year Project\practice\rn-test-appexpo start
Starting project at D:\Final Year Project\practice\rn-test-app
Expo DevTools is running at http://localhost:19002
Opening DevTools in the browser... (press shift-d to disable)
error Invalid regular expression: /(.\fixtures\.|node_modules[\]react[\]dist[\].|website\node_modules\.|heapCapture\bundle.js|.\tests\.)$/: Unterminated character class.
Run CLI with --verbose flag for more details.
Metro Bundler process exited with code 1
Error: Metro Bundler process exited with code 1
at ChildProcess.(C:@expo\xdl@56.2.8\src\Project.ts:1804:16)
at Object.onceWrapper (events.js:300:26)
at ChildProcess.emit (events.js:210:5)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ start:expo start
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\ANONYMOUS\AppData\Roaming\npm-cache_logs\2019-10-08T21_04_22_912Z-debug.log
内容的提问来源于stack exchange,提问作者Abdullah

