当我尝试在Expo Snack中运行以下代码时,收到错误**“无法解析模块”module://@babel/runtime/helpers/interopRequireDefault.js**“
下面是我的代码HomeScreen.js
import { StyleSheet, Text, View, FlatList } from "react-native";
import axios from "axios";
import { useState, useEffect } from "react";
export default function HomeScreen() {
const [cycles, setCycles] = useState([]);
useEffect(() => {
const fetchData = async () => {
try {
const { data: response } = await axios.get(
"URL"
);
setCycles(response);
} catch (error) {
console.error(error.message);
}
};
fetchData();
}, []);
return (
<View style={styles.container}>
{/* <Text> {JSON.stringify(cycles[0])} </Text> */}
<FlatList
data={cycles}
renderItem={(
{ item } //this part will iterate over every item in the array and return a listItem
) => (
<Text>
{new Date(item.startDate).toLocaleDateString("us-EN")} -{" "}
{new Date(item.endDate).toLocaleDateString("us-EN")}{" "}
</Text>
)}
/>
<Text>Hello</Text>
</View>
);
}
下面是App.js
import { StyleSheet, Text, View, FlatList } from "react-native";
import axios from "axios";
import { useState, useEffect } from "react";
import HomeScreen from "./screens/HomeScreen";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/stack";
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{ title: "Welcome" }}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
Package.json文件
{
"dependencies": {
"react-native-paper": "4.9.2",
"@expo/vector-icons": "^13.0.0",
"expo-constants": "~13.2.4",
"axios": "*",
"@react-navigation/stack": "*",
"@react-navigation/native": "6.0.0",
"react-native-gesture-handler": "~2.5.0",
"react-native-safe-area-context": "4.3.1",
"react-native-screens": "~3.15.0"
}
}
x1c 0d1x从其中一个问题,我发现Unable to resolve module @babel/runtime/helpers/interopRequireDefault
,我试图添加巴别塔,它没有工作。(即它抛出另一个错误如下)
谢谢你的帮助!
1条答案
按热度按时间zzwlnbp81#
当JavaScript运行时无法找到代码所需的模块时,通常会出现“无法解析模块'module://@babel/runtime/helpers/interopRequireDefault. js'"错误。在这种情况下,看起来像是
@babel/runtime
包没有正确安装,或者没有正确包含在代码中。要修复此错误,您需要确保项目中已安装
@babel/runtime
包,并且已将其正确导入到代码中。您可以使用以下命令安装@babel/runtime
包:安装软件包后,需要使用以下语法将其导入代码中:
这将从
@babel/runtime
包导入interopRequireDefault
函数,允许您在代码中使用它,而不会遇到“无法解析模块”错误。如果在安装
@babel/runtime
包并将其正确导入到代码中后仍然遇到该错误,则可能是Expo捆绑或传输该包的方式存在问题。在这种情况下,您可能需要联系Expo支持团队以获得进一步的帮助。