首页 > 解决方案 > 使用 React Native 导入任何依赖项时出错

问题描述

初始化 React Native 项目后,如果我安装并导入任何其他依赖项,运行“react-native run-ios”后将显示此错误消息:

react-native run-ios 的错误消息

例如:我运行“react-native init RNProject”来创建简单的反应原生项目。之后我通过:“npm install --save-dev react-navigation”安装react-navigation,然后我通过“import {createStackNavigator} from 'react-navigation'导入createStackNavigator;” 在 App.js 中,什么也不做。在运行“react-native run-ios”并得到上面的错误之后。从'react-navigation'中删除“import {createStackNavigator};” 将导致应用程序正常运行。

我试过“rm node_modules/react-native/local-cli/core/fixtures/files/package.json”或“rm ./node_modules/react-native/local-cli/core/fixtures/files/package.json ”但是终端显示“没有这样的文件或目录”。

我的环境

| software         | version
| ---------------- | -------
| react-navigation | 2.6.2
| react-native     | 0.56.0
| node             | 8.11.3
| npm              | 5.3.0
| MacOS            | 10.13.5

标签: react-native

解决方案


Problem:

Because you used --save-dev in install cmd which means it will be installed as devDependencies in your project.

Solution: Try just installing with this npm install --save react-navigation which will install it as dependencies;

Look at you package.json if you find react-navigation this in dependencies section and still find same problem try deleting node_modules and npm i


推荐阅读