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);
相关推荐
冰冷的bin7 小时前
【React Native】点赞特效动画组件FlowLikeView
react native·react.js·typescript
懒人村杂货铺15 小时前
[特殊字符] 跨端视频通话实战:腾讯云 TRTC + IM(React Native & Web)
react native·音视频·腾讯云
木西1 天前
React Native DApp 开发全栈实战·从 0 到 1 系列(流动性挖矿-前端部分)
react native·web3·智能合约
小妖怪的夏天2 天前
react native 出现 FATAL EXCEPTION: OkHttp Dispatcher
react native·react.js·okhttp
qczg_wxg2 天前
高阶组件介绍
开发语言·javascript·react native·ecmascript
qczg_wxg4 天前
React Native的动画系统
javascript·react native·react.js
qczg_wxg4 天前
React Native常用的API
react native
Winson℡4 天前
在 React Native 层禁止 iOS 左滑返回(手势返回/手势退出)
react native·react.js·ios
卸任4 天前
从0到1搭建react-native自动更新(OTA和APK下载)
前端·react native·react.js
wayne2144 天前
「原生 + RN 混合工程」一条命令启动全攻略:解密 react-native.config.js
android·react native