React Native启动项目捆绑失败,出现Unexpected Token错误

时间:2017-08-17 01:40:27

标签: node.js react-native babel

我正尝试使用https://facebook.github.io/react-native/docs/getting-started.html上的说明为Android创建我的第一个React Native应用。设置依赖项并连接我的Android设备后,我运行了

react-native init app
cd app
react-native run-android

这会启动React Packager,它无法捆绑此错误:

  

错误:捆绑失败:" TransformError:/Users/luky/dev/app/index.android.js:意外令牌)(处理预设时:\" / Users / luky / dev / app /node_modules/babel-preset-react-native/index.js \")"

我正在使用:

  • OSX El Capitan 10.11.6。
  • node version 6.9.2
  • react-native version 0.47.1。

我将生成的项目(包括node_modules)推送到Github以防它有用:https://github.com/luky1971/react-native-app

有谁知道为什么我可能会收到意外的令牌错误?任何帮助将不胜感激!

7 个答案:

答案 0 :(得分:18)

我得到了同样的东西,看起来反应原生的babel预设的最新版本有一些错误(版本3.0.0)。我进入了我的package.json并用这个替换了最新版本:

" babel-preset-react-native":" 2.1.0"。

然后我删除了node_modules目录并运行了npm install并且它运行了。

答案 1 :(得分:4)

安装babel-preset-react-native@2.1.0并运行项目。

在当前项目中,执行此操作...

yarn remove babel-preset-react-native
yarn add babel-preset-react-native@2.1.0

这对我有用,我希望它也适合你。

您可以在https://github.com/facebook/react-native/issues/15513#issuecomment-322824346

找到有关错误的更多信息

答案 2 :(得分:1)

Looking for JS files in
   /Users/admin/Desktop/CoinMarketCap 


React packager ready.

Loading dependency graph, done.
Bundling `index.ios.js`  [development, non-minified, hmr disabled]  0.0% (0/1), failed.
error: bundling failed: "TransformError: /Users/admin/Desktop/CoinMarketCap/index.ios.js: Unexpected token ) (While processing preset: \"/Users/admin/Desktop/CoinMarketCap/node_modules/babel-preset-react-native/index.js\")"

iMac-D001:CoinMarketCap admin$ react-native --v
react-native-cli: 2.0.1
react-native: 0.47.1

为我修好了: https://github.com/facebook/react-native/issues/15545

iMac-D001:CoinMarketCap admin$ npm uninstall babel-preset-react-native
iMac-D001:CoinMarketCap admin$ npm install babel-preset-react-native@2.1.0

答案 3 :(得分:0)

我克隆了你的项目。它工作正常。您可以删除node_modules文件夹,然后删除npm_install。它会正常工作。

答案 4 :(得分:0)

尝试删除根目录中的 .babelrc 文件。或者重命名它。然后重新运行“react-native run-android”命令。这对我有用。祝你好运。

答案 5 :(得分:0)

尝试设置一个新项目,选择如下所示的react-native版本:

  

react-native init --version =“ 0.55.0” MyNewApp

转到新的项目目录。

  

CD项目名称

在android studio中打开应用。打开AVD Manager仿真器,然后选择虚拟设备。然后在提示符处执行以下命令:

  

react-native run-android

这对我有用。希望它能对您有所帮助。

答案 6 :(得分:0)

这也可能是由于您正在使用的依赖项,需要将其置于babel.config.js的预设中

例如我使用EXPO,然后弹出。其中一个依赖项是react-native-paper,为此我不得不更改babel.config.js文件以使其正常工作。

相关问题