Module not found: Can't resolve '@react-navigation/stack' React-Native

Im trying to set a react-native project, in the project i tried only to create stack navigator in app.js with package react-navigation, I follow this guide https://reactnavigation.org/docs/getting-started and then into https://reactnavigation.org/docs/hello-react-navigation but Im getting error

Failed to compile. 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' this is my code :

//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;
/// 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
}

Please Note I tried to npm install, restart project, deleting and installing and all kind of suggestions I found on the web but it didn't work.

Thank you so much for your help!.


Solution 1:

Basically if you run npm install, it tries to pick up the dependencies to install from your package.json. But the problem is @react-navigation/stack is not there.

You need to install that dependency in order to use. Thus try to install that module as the following:

npm install @react-navigation/stack

Solution 2:

Use this

    npm i react-navigation-stack

Solution 3:

it seems like your hadn't install this package When working with nodejs projects, make sure you install all the needed packages, there you just have to install @react-navigation/stack so just run

$ npm i --save @react-navigation/stack

The save option is not required but important if you'll need the same package in differents RN projects Hope it'll help