React Native启动报错:Unexpected token { 问题求助
Hey there, let's get that react-native start error sorted out. That unexpected token { error in @babel/core/lib/transformation/file/file.js is almost always caused by mismatched Babel dependency versions with your React Native 0.54.2 setup. Let's break down the fix step by step:
Problem Breakdown
Your current babel-preset-react-native@4.0.0 and babel-jest@23.0.0-alpha.0 are too new for RN 0.54.2. The 4.x series of babel-preset-react-native is built for React Native 0.55+ and uses Babel 7 syntax that your older RN setup can't properly handle—hence the syntax error when parsing the constructor.
Step-by-Step Solution
1. Downgrade Babel Dependencies to Compatible Versions
First, update your package.json's devDependencies section to use versions that play nice with RN 0.54.2:
"devDependencies": { "babel-jest": "22.4.3", "babel-preset-react-native": "3.0.2", "jest": "22.4.3", "react-test-renderer": "16.3.0-alpha.2" }
- We're sticking with
babel-preset-react-native@3.x(the stable version for RN 0.54.x) - Matching
babel-jestandjestversions ensures no conflicts between your test runner and compiler.
2. Fully Clean and Reinstall Dependencies
Old cache or leftover node modules can mess things up, so run these commands to start fresh:
# Delete existing node modules rm -rf node_modules # Clear npm's cache (force to make sure it's gone) npm cache clean --force # If you use Yarn instead, run this instead of the npm cache command: # yarn cache clean # Reinstall all dependencies npm install
3. Check Your Node.js Version
React Native 0.54.2 works best with Node.js 8.x LTS (like 8.17.0). If you're running Node 10+ or a version older than 8, you might run into additional compatibility issues. Use a version manager like nvm to switch easily:
# Install Node 8.17.0 (if you don't have it) nvm install 8.17.0 # Switch to this version nvm use 8.17.0
4. Reset Metro's Cache
Sometimes Metro bundler holds onto old cached code, so start it with a cache reset:
react-native start --reset-cache
Why This Works
The core issue was that your Babel preset version was too advanced for your React Native version. RN 0.54.2 wasn't built to handle the Babel 7 syntax introduced in babel-preset-react-native@4.0.0, which caused the syntax error when parsing the constructor in the Babel core file. By downgrading to the compatible preset version and aligning your Jest dependencies, you're matching the tooling stack that RN 0.54.2 expects.
内容的提问来源于stack exchange,提问作者Cread Dotson

