使用npx创建React Native项目失败,报SyntaxError语法错误
解决
npx react-native init project执行失败的Unexpected end of input错误 问题详情
执行npx react-native init project创建React Native项目时触发语法错误,已移除全局react-native-cli但问题未解决,错误堆栈如下:
.npm/_npx/7930a8670f922cdb/node_modules/metro/src/Server.js:736 ? delta.added.size + revision.prepend SyntaxError: Unexpected end of input at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1055:15) at Module._compile (node:internal/modules/cjs/loader:1090:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Module.require (node:internal/modules/cjs/loader:1028:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (/path/.npm/_npx/7930a8670f922cdb/node_modules/metro/src/shared/output/bundle.flow.js:14:16) at Module._compile (node:internal/modules/cjs/loader:1126:14)
可行的修复步骤
- 清理npm缓存:执行
npm cache clean --force,清除缓存后重新运行初始化命令。 - 指定稳定版React Native初始化:当前默认版本的metro依赖可能存在语法问题,尝试指定已知稳定版本创建项目,例如:
npx react-native init project --version 0.72.6(可根据官方文档替换为其他兼容版本)。 - 删除npx临时依赖目录:手动删除错误信息中显示的
/.npm/_npx/7930a8670f922cdb/临时文件夹,然后重新执行初始化。 - 检查Node.js版本:确保使用React Native官方推荐的LTS版本(如18.x或20.x),版本不兼容会导致依赖编译失败。
内容的提问来源于stack exchange,提问作者Babou
相关产品推荐
相关产品推荐

