React Native无法解析模块@react-native/assets/registry

Kin*_*ley 1 reactjs react-native

我\xe2\x80\x99 在我的 React Native (v0.68.2) 项目中已经面临一个奇怪的错误一周了,我真的很感谢任何帮助它让我发疯。

\n

由于某种原因,node_modules 中 @react-native 文件夹内的 asset 文件夹似乎自行删除,导致此错误:

\n

Error: unable to resolve module @react-native/assets/registry from /Users/xx/ApplicationPath/node_modules/react-native/Libraries/Image/resolveAssetSource.js: @react-native/assets/registry could not be found within the project or in these directories:

\n

据我所知,当我删除node_modules文件夹并运行时,yarn或者npm install资产文件夹在@react-native中正确安装时,但一段时间后它会随机被删除,我不知道为什么。@react-navigation/elements/src 文件夹中发生类似的事情,资产文件夹也被删除。

\n

删除node_modules并运行yarn后\n

\n

几分钟后

\n

\n

小智 5

检查你的 Metro.config.js

对于较新的 React Native 版本

const path = require("path");
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");

const defaultConfig = getDefaultConfig(__dirname);
const { resolver: { sourceExts, assetExts } } = defaultConfig;

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {
  transformer: {
    babelTransformerPath: require.resolve("react-native-svg-transformer"),
  },
  resolver: {
    assetExts: assetExts.filter((ext) => ext !== "svg"),
    sourceExts: [...sourceExts, "svg"],
  },
  watchFolders: [path.resolve(__dirname, "../")],
};

module.exports = mergeConfig(defaultConfig, config);
Run Code Online (Sandbox Code Playgroud)

它应该是这样的,然后通过 npm start --reset-cache 重置缓存

它对我有用。