You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native项目中@react-navigation/stack找不到的问题求助

问题

我在跟着线上教程开发React Native项目时遇到报错,错误信息如下:

\DUH\src\navigation\Navigator.tsx: @react-navigation/stack could not be found within the project or in these directories:
  node_modules
  ..\..\..\..\node_modules
   6 |   createStackNavigator,
   7 |   StackNavigationProp,
>  8 | } from '@react-navigation/stack';
     |         ^
   9 |
  10 | import * as routes from './routes';
  11 | import {SelectImageScreen} from '../screens/SelectImageScreen';

我的package.json内容:

{
  "name": "DUH",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-navigation/native": "^6.1.8",
    "react": "18.2.0",
    "react-native": "0.72.6",
    "react-native-gesture-handler": "^2.13.2",
    "react-navigation-stack": "^2.10.4"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/eslint-config": "^0.72.2",
    "@react-native/metro-config": "^0.72.11",
    "@tsconfig/react-native": "^3.0.0",
    "@types/react": "^18.0.24",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.2.1",
    "eslint": "^8.19.0",
    "jest": "^29.2.1",
    "metro-react-native-babel-preset": "0.76.8",
    "prettier": "^2.4.1",
    "react-test-renderer": "18.2.0",
    "typescript": "4.8.4"
  },
  "engines": {
    "node": ">=16"
  }
}

我已经安装了react-navigation-stack@^2.10.4,但仍出现上述错误,该如何修复?

修复方案
  • 你安装的react-navigation-stack是React Navigation v4及以下版本的栈导航库,但项目中使用的是React Navigation v6,v6对应的栈导航包是@react-navigation/stack,二者是完全不同的包,因此会出现模块找不到的错误。
  • 执行以下步骤修复:
    1. 卸载旧版栈导航包:
      npm uninstall react-navigation-stack
      
    2. 安装v6对应的栈导航包及依赖原生库:
      npm install @react-navigation/stack
      npm install react-native-screens react-native-safe-area-context
      
    3. 重启Metro服务器并清除缓存:
      npx react-native start --reset-cache
      
    4. 检查并调整导航代码,确保符合React Navigation v6的语法规范(v6与旧版API存在差异)。

内容的提问来源于stack exchange,提问作者YoruLocalMcDonaldsEmployee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 08:40:58