【问题标题】:How to resolve "Duplicate module name: react-native" using Expo如何使用 Expo 解决“重复的模块名称:react-native”
【发布时间】:2019-02-21 17:05:31
【问题描述】:

我在使用自己的 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.

duplicate-module-name-npmpackage.json 是:

{
  "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
}

我已经尝试过here提到的解决方案,即添加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\/.*/,
    ])
  },
};

https://github.com/zxl634/duplicate-module-name-npmhttps://github.com/zxl634/duplicate-module-name 上提供带有 MWE 的存储库。

运行expo start后出现错误。

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

【问题讨论】:

  • 您应该修改其中一个具有 react-native 作为名称的 package.json 的名称。将其更改为 react-native2 以查看是否可以解决它,如果您想要快速解决方案
  • 确实,在duplicate-module-name-npm 中进行更改并在rm -rf node_modules && npm i 中进行更改似乎可以解决问题
  • 不幸的是,该解决方案不起作用,因为两者都需要 react-native 并抱怨如果将其中任何一个更改为例如无法找到它`react-native2`
  • 尝试这里提到的解决方案:*.com/questions/52905842/…,即将reactreact-native 放入devDependencies - 但是,没有用
  • @FrancoCoronel,抱歉,我可能误会了你——你的意思是要更改package.json 的名称吗?

标签: react-native npm expo


【解决方案1】:

通过将路径添加到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.

【讨论】: