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

Viewed 20300

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!.

6 Answers

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

Use this

    npm i react-navigation-stack

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

I was also facing that Issue but after perform basically 2 steps that problem was resolved :)

1.Install the react-navigation-stack using command prompt

project_directory_path\AwesomeProject>npm install @react-navigation/stack

sometimes we get an error "unable to resolve gesture-handler/safe-area-context " so for that first we need to add some other dependencies

->npm install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view

Now hit your Command to run the Build on Device

project_directory_path\AwesomeProject>npx react-native run-android

If you still get errors, here are the answers.

Copy this and past in your VS code terminal.

npm install @react-navigation/native
npm install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
npm install @react-navigation/stack


you have to import modules like this:

import { createStackNavigator } from "react-navigation-stack";
import { createAppContainer } from "react-navigation";
Related