首页 > 解决方案 > 错误:jest-haste-map:Haste 模块命名冲突:

问题描述

我创建了一个自定义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我做了所有可能的方法,但没有任何效果。

我也试过但没有成功

标签: androidreact-nativenpmnpm-installyarnpkg

解决方案


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

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

看到这个问题

希望这对你有用


推荐阅读