错误:jest-haste-map:哈希模块命名冲突:

时间:2019-01-09 10:45:18

标签: android react-native npm npm-install yarnpkg

我创建了一个自定义npm module(将使用 xxx 代替其名称),并使用npm install手动将其链接。

我非常努力地搜寻:

提出问题之前。如果有人告诉我我的代码或方法有什么问题或代码中的任何错误,我将不胜感激。

当我运行react-native run-android时,metro bundler引发以下错误

Error: jest-haste-map: Haste module naming collision:
  Duplicate module name: react-native
  Paths: E:\cdg-native\CDG\node_modules\react-native-XXX\node_modules\react-native\package.json collides with E:\cdg-native\CDG\node_modules\react-native\package.json

This error is caused by `hasteImpl` returning the same name for different files.

我的自定义模块package.json

{
  "name": "react-native-xxx",
  "version": "1.0.0",
  "description": "Library to render xxx",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "react native xxx"
  ],
  "author": "Firdous Nath",
  "license": "ISC",
  "peerDependencies": {
    "react": "*",
    "react-native": "*"
  },
  "devDependencies": {
    "react": "^16.6.1",
    "react-native": "^0.57.5",
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1"
  }
}
定制模块的

index.js非常简单,如下所示

import React from "react";
import { Text } from "react-native";

export default class XXXView extends React.Component {

    render() {
        return (
            <Text> From custom module </Text>
        );
    }
}

我使用自定义模块的文件是

import React from "react";
import {StyleSheet, View} from "react-native";
import XXXView from "react-native-xxx"
//import {XXXView} from "react-native-xxx" -> I tried this as well

export default class App extends React.Component {
    render() {
        return (
            <View style={styles.container}>
                <XXXView/>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: "#f5fcff"
    }
});

我尝试了npm install /absolute/path/to/xxx,它正确地链接了模块。正确地说,我可以在react-native-xxx目录中看到nodemodule软件包。 我做了所有可能的方法,但没有任何效果。

我也尝试过但没有成功

  • 纱线添加/ absolute / path / to / react-native-xxx
  • react-native链接react-native-xxx
  • react-native run-android

3 个答案:

答案 0 :(得分:2)

在根项目中添加 rn-cli.config.js

const blacklist = require('metro-config/src/defaults/blacklist');
module.exports = {
 resolver: {
    blacklistRE: blacklist([
        /node_modules\/.*\/node_modules\/react-native\/.*/,
    ])
 },
};

see this issue

希望它对您有用

答案 1 :(得分:1)

您收到的错误表明您有两个react-native依赖项。您的主项目中的一个,您的xxx模块中的一个,因此在它们的package.json之间产生了冲突。好像是从本地路径安装软件包时,它将复制其node_modules目录。

由于您已经在自定义模块的react-native中将package.json作为对等依赖项,请尝试删除E:\cdg-native\CDG\node_modules\react-native-XXX\node_modules,这应该可以解决冲突。

答案 2 :(得分:1)

看起来您有2个不同的react-native项目文件夹,其中一个文件夹依赖于另一个文件夹(它被包含为node_module依赖项),并且似乎您正在从以下位置运行服务器启动(“ react-native start”)命令库文件夹。