【问题标题】:react-native: Command `run-android` unrecognized. Maybe caused by npm installreact-native:无法识别命令“run-android”。可能是由 npm install 引起的
【发布时间】:2017-11-21 14:18:04
【问题描述】:

最近我开始遇到这个问题,当我安装一个 react-native 包时 (例如:react-navigation)进入我的项目,一大堆包被删除(包括反应,我认为反应原生)。

然后当我尝试运行命令“run-android”时,它说它无法识别。

我最近更新到最新的npm 和react-native-cli。 “npm install”有问题吗?还是react-native?

node version: 8.1.2 <br/>
react-native-cli: 2.0.1 <br/>
react-native: 0.45.1 <br/>
react-navigation: 1.0.0-beta.11

以下是重新创建的步骤:

  • 第 1 步 - 创建项目。

  • 第 2 步 - 运行“run-android”命令(可行)。

  • 第 3 步 - 将“react-native-navigation”安装到项目中。

Notice in the image above. Seems like all the other packages are removed from the project.<br/><br/>
  • 第 4 步 - 尝试再次运行“run-android”命令。 (会失败,但以前可以工作)

您知道问题是什么以及解决方法吗?

【问题讨论】:

  • 你安装了 react-native-cli 吗?
  • 是的@alejandrogarciarobles。我用版本信息更新了问题。
  • 您是否检查过是否从 node_modules 中的 react native 中删除了任何内容?
  • 是的@Raymond。除了react-navigation(根据上面的例子)之外的所有包都被清空了。里面剩下的文件很少。
  • npm install 命令将起作用

标签: android react-native npm npm-install react-native-navigation


【解决方案1】:

找到解决方案here。

一开始运行 npm install 不起作用,但后来删除 package-lock.json 文件并运行 npm install 完成了这项工作。

之后我单独安装了react-navigation 包,它运行良好。

【讨论】:

  • 好像在最初创建项目的时候,如果安装了yarn,后面安装其他包/依赖的时候会和npm冲突。这就是为什么我们必须使用 npm install 删除并重新安装 node_module 包(只需一次)。
【解决方案2】:

这就是从头到尾对我有用的东西。

  1. react-native init NavTest (使用此命令在本地安装 cli)
  2. 已删除 package-lock.json
  3. npm install --save react-navigation
  4. 删除了生成的package-lock.json
  5. npm install
  6. react-native run android 有点难看,我不完全知道发生了什么,但这有效。 https://reactnavigation.org/ 用于运行示例代码。

或将其复制到 index.android.js

import React, { Component } from 'react';
import { 
  AppRegistry,
  Button,
} from 'react-native';
import {
  StackNavigator,
} from 'react-navigation';



class HomeScreen extends Component {
  static navigationOptions = {
    title: 'Welcome',
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <Button
        title="Go to Jane's profile"
        onPress={() =>
          navigate('Profile', { name: 'Jane' })
        }
      />
    );
  }
}

class ProfileScreen extends Component{
  static navigationOptions = {
    title: 'Jane',
  };
  render() {
    return (null);
  }
}

const NavTest= StackNavigator({
  Home: { screen: HomeScreen },
  Profile: { screen: ProfileScreen },
});

AppRegistry.registerComponent('NavTest', () => NavTest);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 2016-10-03
    • 2018-03-07
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多