反应本机突然无法解决模块`fs`错误

react native sudden Unable to resolve module `fs` error

我突然收到模块 'fs' 无法解析的错误。但我没有使用过这个模块,也没有改变任何东西。我的应用程序在启动时突然 returns 这个错误。

error: bundling failed: Error: Unable to resolve module fs from /Path/to/Project/node_modules/handlebars/lib/index.js: Module fs does not exist in the Haste module map

This might be related to https://github.com/facebook/react-native/issues/4968 To resolve try the following:

  1. Clear watchman watches: watchman watch-del-all.
  2. Delete the node_modules folder: rm -rf node_modules && npm install.
  3. Reset Metro Bundler cache: rm -rf /tmp/metro-bundler-cache-* or npm start -- --reset-cache.
  4. Remove haste cache: rm -rf /tmp/haste-map-react-native-packager-*.

我已经试过四步解决,试了好几次都没有用。 有什么想法可能是错误的吗?

通常您可以通过清除缓存来修复这些意外错误。 运行 $ expo start --clear.

我遇到这个错误是因为我试图使用 dotenv when I should have been using 。检查您安装的所有软件包是否与 React Native 兼容。

对我来说,问题是 VSCode 在我的一个 js 文件的顶部插入了一些导入。很奇怪。 这些是行:

import { tsConstructorType } from '@babel/types';
import { logger } from 'handlebars';

我正好遇到了这个问题。 Visual Studio 代码用户,自动完成有时会在您不注意的情况下自动导入您文件顶部不需要的模块。 运行ning git diff 揭示了以下几行我不记得我处理过的文件顶部的每一个文字:

+import { clearConfigCache } from 'prettier';
+import { createIconSetFromFontello } from 'react-native-vector-icons';

如何避免:

运行 git diff 并阅读后面的每一行。违规者通常会出现。

我的一个节点模块依赖于 react-native-dotenv,但它的代码使用的是像 import {} from 'react-native-dotenv' 这样的旧导入。 但是最新版本的 dotenv 正在使用 import {} from '@env'。 在模块中修复此导入解决了问题。

如果您要在 expo react native 中导入模块(来自另一个文件的 functional/class 组件),请务必在 assets 数组中提及“./” rnpm 对象在 package.json 文件中像这样:

"rnpm": {
    "assets": [
      "./"
    ]
  }

不要尝试单独安装“fs”模块,否则会出现更多错误。

我的 auto-suggestion 不小心将它导入了我的文件,导致了同样的问题。

import { status } from "express/lib/response";

我删除了并且有效。 尝试在导入中找到不应该存在的东西,比如后端导入。