Unable to resolve module missing-asset-registry-path from XXX

问题:

在我将老项目(0.67.0)升级到项目(0.74.1)时打包报错

bash 复制代码
PS D:\tiro\iDealLedRN> npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/assets/
warn Package react-native-sqlite-storage contains invalid configuration: "dependency.platforms.ios.project" is not allowed. Please verify it's properly linked using "npx react-native config" command and contact the package maintainers about this.
warn =================================================================================================
warn From React Native 0.73, your project's Metro config should extend '@react-native/metro-config'
warn or it will fail to build. Please copy the template at:
warn https://github.com/facebook/react-native/blob/main/packages/react-native/template/metro.config.js
warn This warning will be removed in future (https://github.com/facebook/metro/issues/1018).
warn =================================================================================================
                Welcome to Metro v0.80.9
              Fast - Scalable - Integrated


error Unable to resolve module missing-asset-registry-path from D:\tiro\iDealLedRN\res\arrow.png: missing-asset-registry-path could not be found within the project or in these directories:
  node_modules
> 1 | �PNG
  2 | 
  3 |
  4 | IHDR�<q�sRGB���IDATx^�ݹ�UE�#BB$�        OA.
Error: Unable to resolve module missing-asset-registry-path from D:\tiro\iDealLedRN\res\arrow.png: missing-asset-registry-path could not be found within the project or in these directories:
  node_modules
> 1 | �PNG
  2 | 
  3 |
  4 | IHDR�<q�sRGB���IDATx^�ݹ�UE�#BB$�        OA
    at ModuleResolver.resolveDependency (D:\tiro\iDealLedRN\node_modules\metro\src\node-haste\DependencyGraph\ModuleResolution.js:138:15)
    at DependencyGraph.resolveDependency (D:\tiro\iDealLedRN\node_modules\metro\src\node-haste\DependencyGraph.js:231:43)
    at D:\tiro\iDealLedRN\node_modules\metro\src\lib\transformHelpers.js:156:21
    at resolveDependencies (D:\tiro\iDealLedRN\node_modules\metro\src\DeltaBundler\buildSubgraph.js:42:25)
    at visit (D:\tiro\iDealLedRN\node_modules\metro\src\DeltaBundler\buildSubgraph.js:83:30)
    at async Promise.all (index 0)
    at async visit (D:\tiro\iDealLedRN\node_modules\metro\src\DeltaBundler\buildSubgraph.js:92:5)
    at async Promise.all (index 3)
    at async visit (D:\tiro\iDealLedRN\node_modules\metro\src\DeltaBundler\buildSubgraph.js:92:5)
    at async Promise.all (index 3)
info Run CLI with --verbose flag for more details.

其实这里早有提示

bash 复制代码
warn From React Native 0.73, your project's Metro config should extend '@react-native/metro-config'
warn or it will fail to build. Please copy the template at:
warn https://github.com/facebook/react-native/blob/main/packages/react-native/template/metro.config.js
warn This warning will be removed in future (https://github.com/facebook/metro/issues/1018).

从React Native 0.73开始,您项目的Metro配置应该扩展"@react-native/metro-config" 警告否则它将无法构建。

解决:

更改根目录metro.config.js内容使其扩展"@react-native/metro-config"

javascript 复制代码
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');

/**
 * Metro configuration
 * https://reactnative.dev/docs/metro
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

拓展:

比如 通过metro进行的JS Bundle拆包 https://www.jianshu.com/p/18d6ed19ff10 配置metro.common.config.js 和 metro.business.config.js 也需要 扩展"@react-native/metro-config"

metro.common.config.js

javascript 复制代码
// const fs = require('fs');
// const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');

// function getModuleId(path) {
//   // 根据文件的相对路径构建 moduleId
//   const projectRootPath = __dirname;
//   let moduleId = path.substr(projectRootPath.length + 1);
//   return moduleId;
// }

// function createModuleIdFactory() {
//   return getModuleId;
// }

// function processModuleFilter(module) {
//   const moduleId = getModuleId(module['path']);
//   // 把 moduleId 写入 moduleIdList.txt 文件,记录基础模块的 moduleId
//   fs.appendFileSync('./moduleIdList.txt', `${moduleId}\n`);
//   return true;
// }

// const config = {
//   serializer: {
//     createModuleIdFactory,
//     processModuleFilter,
//   },
// };

// module.exports = mergeConfig(getDefaultConfig(__dirname), config);


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

function getModuleId(path) {
  // 根据文件的相对路径构建 moduleId
  const projectRootPath = __dirname;
  let moduleId = path.substr(projectRootPath.length + 1);
  return moduleId;
}

function createModuleIdFactory() {
  return getModuleId;
}

function processModuleFilter(module) {
  const moduleId = getModuleId(module['path']);
  // 把 moduleId 写入 moduleIdList.txt 文件,记录基础模块的 moduleId
  fs.appendFileSync('./moduleIdList.txt', `${moduleId}\n`);
  return true;
}

const config = {
  serializer: {
    createModuleIdFactory,
    processModuleFilter,
  },
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

metro.business.config.js

javascript 复制代码
// const fs = require('fs');

// // 读取 moduleIdList.txt,转换为数组
// const moduleIdList = fs.readFileSync('./moduleIdList.txt', 'utf8').toString().split('\n');

// function getModuleId(path) {
//     // 根据文件的相对路径构建 moduleId
//     const projectRootPath = __dirname;
//     let moduleId = path.substr(projectRootPath.length + 1);
//     return moduleId;
// }

// function createModuleIdFactory() {
//     return getModuleId
// }

// function processModuleFilter(module) {
//     //const modulePath = getProjectPath(module['path'])
//     const moduleId = getModuleId(module['path'])
//     if (moduleIdList.indexOf(moduleId) >= 0) {
//         // 过滤掉上一步记录的基础模块的moduleId
//         return false;
//     }
//     return true;
// }

// module.exports = {
//     serializer: {
//         createModuleIdFactory,
//         processModuleFilter,
//     }
// };

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

// 读取 moduleIdList.txt,转换为数组
const moduleIdList = fs.readFileSync('./moduleIdList.txt', 'utf8').toString().split('\n');

function getModuleId(path) {
    // 根据文件的相对路径构建 moduleId
    const projectRootPath = __dirname;
    let moduleId = path.substr(projectRootPath.length + 1);
    return moduleId;
}

function createModuleIdFactory() {
    return getModuleId;
}

function processModuleFilter(module) {
    //const modulePath = getProjectPath(module['path'])
    const moduleId = getModuleId(module['path']);
    if (moduleIdList.indexOf(moduleId) >= 0) {
        // 过滤掉上一步记录的基础模块的 moduleId
        return false;
    }
    return true;
}

const config = {
    serializer: {
        createModuleIdFactory,
        processModuleFilter,
    }
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);
相关推荐
木西6 小时前
使用 React Native 中的 FlatList 实现下拉刷新、滚动加载更多及定时刷新的长列表
react native·react.js
君的名字5 天前
怎么判断一个Android APP使用了taro 这个跨端框架
android·react native·taro
谢尔登8 天前
【React Native】快速入门
javascript·react native·react.js
进取星辰8 天前
32、跨平台咒语—— React Native初探
javascript·react native·react.js
君的名字8 天前
怎么判断一个Android APP使用了React Native 这个跨端框架
android·react native·react.js
iamtsfw8 天前
从头实现react native expo本地生成APK
javascript·react native·react.js
Bruce_Liuxiaowei9 天前
HarmonyOS NEXT~React Native 在鸿蒙系统上的应用与实践
react native·华为·harmonyos
程序猿阿伟12 天前
《虚拟即真实:数字人驱动技术在React Native社交中的涅槃》
javascript·react native·react.js
xx240613 天前
React Native简介
javascript·react native·react.js
程序猿阿伟13 天前
《社交应用动态表情:RN与Flutter实战解码》
javascript·flutter·react native