Home > Software engineering >  Error: Unable to resolve module react-navigation-stack from /src/navigation/index.js:
Error: Unable to resolve module react-navigation-stack from /src/navigation/index.js:

Time:01-02

I'm a react-native beginner.

I was doing stack screen. Then I've got the below error.

 Error: Unable to resolve module react-navigation-stack from /Users/chosohee/app2/src/navigation/index.js: react-navigation-stack could not be found within the project or in these directories:
  node_modules
  ../node_modules
  1 | import { createAppContainer, createSwitchNavigator } from 'react-navigation';
> 2 | import { createStackNavigator } from 'react-navigation-stack';
    |                                       ^
  3 | import LoginScreen from './LoginScreen';
  4 | import HomeScreen from './HomeScreen';
  5 |
    at ModuleResolver.resolveDependency (/Users/chosohee/app2/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:178:15)
    at DependencyGraph.resolveDependency (/Users/chosohee/app2/node_modules/metro/src/node-haste/DependencyGraph.js:264:43)
    at Object.resolve (/Users/chosohee/app2/node_modules/metro/src/lib/transformHelpers.js:170:21)
    at resolveDependencies (/Users/chosohee/app2/node_modules/metro/src/DeltaBundler/graphOperations.js:466:33)
    at processModule (/Users/chosohee/app2/node_modules/metro/src/DeltaBundler/graphOperations.js:232:31)
    at async addDependency (/Users/chosohee/app2/node_modules/metro/src/DeltaBundler/graphOperations.js:361:18)
    at async Promise.all (index 7)
    at async processModule (/Users/chosohee/app2/node_modules/metro/src/DeltaBundler/graphOperations.js:279:3)
    at async addDependency (/Users/chosohee/app2/node_modules/metro/src/DeltaBundler/graphOperations.js:361:18)
    at async Promise.all (index 7)
 

Here is my src/navigation/index.js

import { createAppContainer, createSwitchNavigator } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import LoginScreen from './LoginScreen';
import HomeScreen from './HomeScreen';

const AuthStack = createStackNavigator(
    {
        LoginScreen: {screen: LoginScreen},
        HomesScreen: {screen: HomeScreen}
    },
    {
        initialRouteName: 'LoginScreen'
    }
);

const AppNavigator = createSwitchNavigator(
    {
        Auth: AuthStack
    },
    {
        initialRouteName: 'Auth',
    }
);

export default createAppContainer(AppNavigator);

Here is my package.json

{
  "name": "app2",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/masked-view": "0.1.11",
    "@react-navigation/bottom-tabs": "6.5.2",
    "@react-navigation/native": "6.1.1",
    "@react-navigation/native-stack": "6.9.7",
    "@react-navigation/stack": "6.3.10",
    "react": "18.1.0",
    "react-native": "0.70.6",
    "react-native-gesture-handler": "2.8.0",
    "react-native-reanimated": "2.13.0",
    "react-native-safe-area-context": "4.4.1",
    "react-native-screens": "3.18.2"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/runtime": "^7.12.5",
    "@react-native-community/eslint-config": "^2.0.0",
    "babel-jest": "^26.6.3",
    "eslint": "^7.32.0",
    "jest": "^26.6.3",
    "metro-react-native-babel-preset": "0.72.3",
    "react-test-renderer": "18.1.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

I tried Unable to resolve module 'react-navigation' https://github.com/react-navigation/react-navigation/issues/9976 https://reactnavigation.org/docs/troubleshooting/

when I tried npx react-native start --reset-cache

I got another error below.

error listen EADDRINUSE: address already in use :::8081.
Error: listen EADDRINUSE: address already in use :::8081
    at Server.setupListenHandle [as _listen2] (node:net:1717:16)
    at listenInCluster (node:net:1765:12)
    at Server.listen (node:net:1853:7)
    at /Users/chosohee/app2/node_modules/metro/src/index.flow.js:398:14
    at new Promise (<anonymous>)
    at earlyPortCheck (/Users/chosohee/app2/node_modules/metro/src/index.flow.js:394:11)
    at exports.runServer (/Users/chosohee/app2/node_modules/metro/src/index.flow.js:155:9)
    at Object.runServer [as func] (/Users/chosohee/app2/node_modules/@react-native-community/cli-plugin-metro/build/commands/start/runServer.js:123:49)
    at async Command.handleAction (/Users/chosohee/app2/node_modules/@react-native-community/cli/build/index.js:142:9)
info Run CLI with --verbose flag for more details.

CodePudding user response:

Your import is not correct according to the stack navigation documentation you need to import createStackNavigator from @react-navigation/stack not from react-navigation-stack like this:

import { createStackNavigator } from '@react-navigation/stack';

For more information you can check the documentation from here.

  • Related