运行 expo start 时无法解析模块 fs
Posted
技术标签:
【中文标题】运行 expo start 时无法解析模块 fs【英文标题】:Unable to resolve module fs when running expo start 【发布时间】:2021-09-18 10:08:33 【问题描述】:我一生都无法弄清楚为什么我的应用突然无法启动。我不断收到此错误:
无法解析模块 fs 来自
node_modules\firebase-admin\lib\firebase-namespace.js
: fs 不能 可以在项目中找到。 如果您确定该模块存在,请尝试以下步骤:清空守望者手表:watchman watch-del-all 删除 node_modules 并运行 yarn install 重置 Metro 的缓存:yarn start --reset-cache 删除缓存:rm -rf /tmp/metro-*
每当我在根文件夹中运行 expo start
时。我什至尝试过expo start -c
重置缓存。
我还尝试删除 node_modules 并为 myapp/functions/node_modules 和 myapp/node_modules 重新安装它。
我已尝试更新 firebase-admin 和所有依赖项。
这很奇怪,因为我的应用几天前还在工作,而这突然出现了。我以前从未安装过 fs。
有人知道这里发生了什么吗?感觉像是一个简单的环境修复,但我不确定。
如果有帮助,我的应用程序依赖项:
"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",
"test": "jest"
,
"dependencies":
"@react-native-async-storage/async-storage": "^1.13.0",
"@react-native-community/masked-view": "0.1.10",
"algoliasearch": "^4.8.3",
"axios": "^0.21.1",
"buffer": "^6.0.3",
"expo": "^41.0.0",
"expo-font": "~9.1.0",
"expo-image-picker": "~10.1.4",
"expo-linear-gradient": "~9.1.0",
"expo-notifications": "~0.11.6",
"expo-status-bar": "~1.0.4",
"expo-web-browser": "~9.1.0",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz",
"react-native-elements": "^3.0.0-alpha.1",
"react-native-gesture-handler": "~1.10.2",
"react-native-keyboard-aware-scroll-view": "^0.9.3",
"react-native-linear-gradient": "^2.5.6",
"react-native-paper": "^4.7.0",
"react-native-reanimated": "^1.13.3",
"react-native-screens": "^2.10.1",
"react-native-snap-carousel": "^3.9.1",
"react-native-svg": "12.1.0",
"react-native-svg-transformer": "^0.14.3",
"react-native-vector-icons": "^7.1.0",
"react-navigation": "^4.4.3",
"react-navigation-drawer": "^2.6.0",
"react-navigation-header-buttons": "^6.3.1",
"react-navigation-stack": "^2.9.0",
"react-navigation-tabs": "^2.10.1",
"react-redux": "^7.2.2",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
,
"devDependencies":
"firebase": "8.2.3",
"jest": "^26.6.3",
"jest-expo": "^41.0.0",
"react-test-renderer": "^17.0.2"
,
"private": true,
"jest":
"preset": "jest-expo",
"transformIgnorePatterns": [
"./node_modules/(?!(react-navigation-header-buttons|react-native|react-native-gesture-handler|@expo)|expo-font|@unimodules|expo-asset|expo-constants|expo-file-system|expo-web-browser|react-navigation|react-navigation-stack|unimodules-permissions-interface|expo-permissions|expo-image-picker|expo-linear-gradient/)"
],
"setupFiles": [
"./node_modules/react-native-gesture-handler/jestSetup.js"
],
"verbose": true,
"updateSnapshot": true
和.babelrc
:
"presets": [
"babel-preset-expo"
]
和firebase函数依赖关系:
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts":
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
,
"engines":
"node": "12"
,
"main": "index.js",
"dependencies":
"algoliasearch": "^4.8.3",
"axios": "^0.21.0",
"cloudinary": "^1.23.0",
"crypto": "^1.0.1",
"events": "^3.3.0",
"express": "^4.17.1",
"firebase": "^8.7.0",
"firebase-admin": "^9.10.0",
"firebase-functions": "^3.14.1"
,
"devDependencies":
"firebase-functions-test": "^0.2.0"
,
"private": true
firebase-admin 然后在我的 myapp/functions/index.js 中初始化为:
const admin = require("firebase-admin");
admin.initializeApp();
然后用作(例如):
// Called for toggling Tutorial Prompt on ProfileScreen
exports.setTutorialPrompt = functions.https.onRequest(async (req, res) =>
res.set("Access-Control-Allow-Origin", "*");
if (req.method === "OPTIONS")
// Send response to OPTIONS requests
res.set("Access-Control-Allow-Methods", "GET, POST");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Access-Control-Max-Age", "3600");
res.status(201).send("CORS preflight options set!");
else
const db = admin.firestore(); // <--------------------------- here
const exampleId, ExampletwoId, value, screen = req.body;
const updatedProfile =
tutorialPrompt: value,
;
const index = await client.initIndex(ALGOLIA_INDEX_NAME);
index
.partialUpdateObject(
objectID: ExhibitUId,
tutorialPrompt: value,
)
.wait();
db.collection("users").doc(localId).update(updatedProfile);
res.status(201).send(`Successfully toggled tutorialing prompt`);
);
【问题讨论】:
fs
是一个节点标准库模块,它在 react-native 环境中不可用,就像在 Web 浏览器中不可用一样。您尝试使用的库是在 node.js 中使用的。
在 node.js 环境中使用。 firebase-admin 库用于 myapp/functions,我使用 node.js 与 firebase 云函数进行通信。感谢您指出这一点,我将编辑我的帖子以显示这一点。
【参考方案1】:
我设法修复它。
这是根应用程序文件夹中的随机导入错误。不知何故,我的一个导入混淆了,并试图调用云函数而不是 redux-action 函数。
错误的导入:
import setTutorialPrompt from "../../functions/index.js";
正确的导入:
import setTutorialPrompt from "../../store/actions/user";
因为它试图导入我的函数文件夹,所以它给了我 fs 错误。类似于@brentvatne 所说的。
如果其他人遇到此错误,我建议将您的云函数文件夹移动到您的桌面或其他地方,并尝试在没有它的情况下运行您的应用程序。这样我就找到了正确的错误代码。
【讨论】:
【参考方案2】:我发现这个是因为我遇到了几乎完全相同的问题。
我发现有(奇怪的)两种方法可以将firebase-admin
导入到 react 原生项目中。我在做import firestore from "firebase-admin"
,我需要做import firestore from '@react-native-firebase/firestore'
一个很容易忽略的区别,所以我想我会在这里发表评论,以防其他人有这个问题。
【讨论】:
以上是关于运行 expo start 时无法解析模块 fs的主要内容,如果未能解决你的问题,请参考以下文章
无法从“/home/pfe/node_modules/@expo/vector-icons/Entypo.js”解析模块“./fonts/Entypo.ttf”
未找到模块:错误:无法解析“../aws-exports”(React-Native Expo Web)
如何修复 npm start “无法确定原生 SDK 版本”错误?