React Native for Android - 使用 ./gradlew assembleRelease 会产生“Cannot find module 'babel-preset-react-a

Posted

技术标签:

【中文标题】React Native for Android - 使用 ./gradlew assembleRelease 会产生“Cannot find module \'babel-preset-react-app\'”错误【英文标题】:React Native for Android - Using ./gradlew assembleRelease will produce “Cannot find module 'babel-preset-react-app' “ errorReact Native for Android - 使用 ./gradlew assembleRelease 会产生“Cannot find module 'babel-preset-react-app'”错误 【发布时间】:2019-06-30 19:39:04 【问题描述】:

-1) 按照官方说明生成发布APK。 a link

-2) 在下面运行此命令。

    $ cd android
    $ ./gradlew assembleRelease

-3) 结果:我收到以下错误消息。

index.js: 找不到模块 'babel-preset-react-app'

错误:找不到模块'babel-preset-react-app'

我正在尝试使用 React Native 为 Android 应用创建发布版本。 我已经搜索了包括以下内容的潜在解决方案。但我还没有找到任何适合我的东西。 Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-react'

[package.json] 第 9 到 41 行

"dependencies": 
"mobx": "^5.9.0",
"mobx-react": "^5.4.3",
"react": "16.6.3",
"react-dom": "^16.7.0",
"react-native": "0.58.3",
"react-native-gesture-handler": "^1.0.15",
"react-native-image-pan-zoom": "^2.1.11",
"react-navigation": "^3.1.2",

"devDependencies": 
"@babel/plugin-proposal-decorators": "^7.3.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.0.0",
"jest": "24.0.0",
"metro-react-native-babel-preset": "0.51.1",
"react-test-renderer": "16.6.3",
"jest": 
"preset": "react-native",

"babel": 
"presets": [
  "react-app"
],


"plugins": [
  [
    "@babel/plugin-proposal-decorators",
    
      "legacy": true
    
  ]
]

[index.js]

import AppRegistry from 'react-native';
import App from './App';
import name as appName from './app.json';

[app.js]

import React, Component, PropTypes from 'react';
import  Platform, StyleSheet, Text, View, Image, Dimensions, Button, TouchableHighlight, WebView  from 'react-native';
import  createStackNavigator, createAppContainer, StackActions, NavigationActions, withNavigation  from "react-navigation";

预期结果:终端将完成本官方指南中描述的过程,并在此文件夹路径下生成一个 apk 文件 android/app/build/outputs/apk/release/app-release.apk

React Native 官方指南: https://facebook.github.io/react-native/docs/signed-apk-android

实际输出: 我收到以下错误消息。

index.js: 找不到模块 'babel-preset-react-app'

错误:找不到模块'babel-preset-react-app'

任何建议将不胜感激。

【问题讨论】:

【参考方案1】:

我找到了解决方案。我会回答我自己的问题... 我从 package.json 中删除了以下行。 而且我现在不再看到此错误消息。

"plugins": [
  [
    "@babel/plugin-proposal-decorators",
    
      "legacy": true
    
  ]
]

【讨论】:

以上是关于React Native for Android - 使用 ./gradlew assembleRelease 会产生“Cannot find module 'babel-preset-react-a的主要内容,如果未能解决你的问题,请参考以下文章

React Native DEMO for Android

React Native For Android 架构初探

React Native For Android 架构初探

React Native For Android 架构初探

游戏环境配置:React Native 开发环境配置 For Android

React native for Android [原理剖析]