找不到模块:无法解析'@react-navigation/stack' React-Native

Ami*_*lon 2 javascript reactjs react-native

我试图设置一个 react-native 项目,在该项目中,我只尝试在 app.js 中使用包 react-navigation 创建堆栈导航器,我按照本指南https://reactnavigation.org/docs/getting-started然后进入https://reactnavigation.org/docs/hello-react-navigation但我收到错误

编译失败。D:/Visual Studio Code/Resturant Review/food/App.js Module not found: Can't resolve '@react-navigation/stack' in 'D:\Visual Studio Code\Resturant Review\food' 这是我的代码:

//app.js

import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';

function HomeScreen() {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
    </View>
  );
}

const Stack = createStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={HomeScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default App;
Run Code Online (Sandbox Code Playgroud)
/// package.json
{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/masked-view": "0.1.10",
    "@react-navigation/native": "^5.7.3",
    "expo": "~38.0.8",
    "expo-status-bar": "^1.0.2",
    "react": "~16.11.0",
    "react-dom": "~16.11.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
    "react-native-gesture-handler": "~1.6.0",
    "react-native-reanimated": "~1.9.0",
    "react-native-safe-area-context": "~3.0.7",
    "react-native-screens": "~2.9.0",
    "react-native-web": "~0.11.7"
  },
  "devDependencies": {
    "@babel/core": "^7.8.6",
    "babel-preset-expo": "~8.1.0"
  },
  "private": true
}

Run Code Online (Sandbox Code Playgroud)

请注意,我尝试npm install重新启动项目、删除和安装以及我在网上找到的各种建议,但没有奏效。

非常感谢你的帮助!。

nor*_*ial 6

基本上,如果您运行npm install,它会尝试从您的package.json. 但问题@react-navigation/stack是不存在。

您需要安装该依赖项才能使用。因此尝试安装该模块如下:

npm install @react-navigation/stack
Run Code Online (Sandbox Code Playgroud)