首页 > 解决方案 > 如何使用 Expo 解决“重复的模块名称:react-native”

问题描述

使用我自己的 npm 包时,我不断收到以下错误,这也取决于react-native

jest-haste-map: @providesModule naming collision:
      Duplicate module name: react-native
      Paths: /reproducible-bug-examples/duplicate-module-name-npm/node_modules/react-native/package.json collides with /reproducible-bug-examples/duplicate-module-name/node_modules/react-native/package.json

    This error is caused by a @providesModule declaration with the same name across two different files.

package.jsonduplicate-module-name-npm:_

{
  "name": "duplicate-module-name-npm",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
    "react": "16.5.0"
  },
}

因为duplicate-module-name它是:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "eject": "expo eject",
    "test": "node ./node_modules/jest/bin/jest.js --watchAll"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "@expo/samples": "2.1.1",
    "expo": "^32.0.0",
    "duplicate-module-name-npm": "file:../duplicate-module-name-npm",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
    "react-navigation": "^3.0.9"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.0.0",
    "jest-expo": "^32.0.0"
  },
  "private": true
}

我已经尝试过这里提到的解决方案,即添加一个rn-cli.config.js,但问题仍然存在。实际上,在添加了此处描述的内容后,我得到了以下错误:

 (node:31392) UnhandledPromiseRejectionWarning: Error: jest-haste-map: @providesModule naming collision:
  Duplicate module name: react-native
  Paths: /reproducible-bug-examples/duplicate-module-name-npm/node_modules/react-native/package.json collides with /reproducible-bug-examples/duplicate-module-name/node_modules/react-native/package.json

This error is caused by a @providesModule declaration with the same name across two different files.

还尝试将以下内容添加到rn-cli-config.js

const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
  resolver: {
    blacklistRE: blacklist([
      /duplicate-module-name-npm\/.*/,
    ])
  },
};

带有 MWE 的存储库可在https://github.com/zxl634/duplicate-module-name-npmhttps://github.com/zxl634/duplicate-module-name获得。

运行后出现错误expo start

非常感谢任何帮助或建议:)

标签: react-nativenpmexpo

解决方案


通过添加路径的组合来解决它rn-cli.config.js

const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
  resolver: {
    blacklistRE: blacklist([
  /duplicate-module-name-npm\/node_modules\/react-native\/Libraries\/Sample\/.*/,
  /duplicate-module-name-npm\/node_modules\/react-native\/react-native-git-upgrade\/.*/,
  /duplicate-module-name-npm\/node_modules\/react-native\/react-native-cli\/.*/,
    ])
  },
};

并更改导致问题的 package.json 文件中的“名称”字段,例如react-native/package.json.


推荐阅读