簡體   English   中英

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

[英]Module not found: Can't resolve '@react-navigation/stack' 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;
/// 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
}

請注意,我嘗試npm install ,重新啟動項目,刪除和安裝以及我在網上找到的所有建議,但沒有奏效。

非常感謝你的幫助!。

基本上,如果您運行npm install ,它會嘗試從您的package.json選擇要安裝的依賴項。 但問題是@react-navigation/stack不存在。

您需要安裝該依賴項才能使用。 因此嘗試安裝該模塊如下:

npm install @react-navigation/stack

好像你還沒有安裝這個包在使用 nodejs 項目時,確保你安裝了所有需要的包,你只需要安裝@react-navigation/stack所以就運行

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

如果您在不同的 RN 項目中需要相同的包,則保存選項不是必需的但很重要希望它會有所幫助

用這個

    npm i react-navigation-stack

我也遇到了這個問題,但基本上執行了 2 個步驟后,問題就解決了:) 1.使用命令提示符安裝 react-navigation-stack

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

2.安裝后我們需要運行一個命令

project_directory_path\AwesomeProject>npx react-native start --port=8080

現在點擊你的命令來運行設備上的構建

project_directory_path\AwesomeProject>npx react-Native run-android

如果您仍然遇到錯誤,這里是答案。

在你的 VS 代碼終端中復制這個和過去。

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


你必須像這樣導入模塊:

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

Bonjour, je suis tomber sur ce même problème et j ai déjà essayer tous ces solutions mais je 'ai pas réussi。 Quelqu'un Pourrai t-il m'aider 高級副總裁?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM