问题:

当试图从命令行运行react-native run-ios时,我的项目有一个@providesModule naming collision.它与另一个npm包esdoc创建的自动生成的dir dist/冲突.我希望能够保留这个自动生成的目录,只需让react-native 打包程序忽略dist/目录.

错误消息:

[01/23/2017, 13:17:07] <START> Building Haste Map
    Failed to build DependencyGraph: @providesModule naming collision:
      Duplicate module name: ann
      Paths: /Users/thurt/projects/example/package.json collides with /Users/thurt/projects/example/dist/esdoc/package.json

This error is caused by a @providesModule declaration with the same name across two different files.
Error: @providesModule naming collision:
  Duplicate module name: ann
  Paths: /Users/thurt/projects/example/package.json collides with /Users/thurt/projects/example/dist/esdoc/package.json

This error is caused by a @providesModule declaration with the same name across two different files.
    at HasteMap._updateHasteMap (/Users/thurt/projects/example/node_modules/react-native/packager/react-packager/src/node-haste/DependencyGraph/HasteMap.js:158:13)
    at p.getName.then.name (/Users/thurt/projects/example/node_modules/react-native/packager/react-packager/src/node-haste/DependencyGraph/HasteMap.js:133:31)

推荐答案

这种配置有在RN版本之间更改的习惯.有关创建配置文件、加载配置文件和清除缓存的版本特定说明,请参见下文.

For React Native >= 0.64(?)

This is speculative as 0.64 is not yet released, 100 is 101 in RN in 0.64.

在项目根目录中创建metro.config.js,内容如下:

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

// exclusionList is a function that takes an array of regexes and combines
// them with the default exclusions to return a single regex.

module.exports = {
  resolver: {
    blacklistRE: exclusionList([/dist\/.*/])
  }
};

For React Native >= 0.59, < 0.64

在项目根目录中创建metro.config.js,内容如下:

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

// blacklist is a function that takes an array of regexes and combines
// them with the default blacklist to return a single regex.

module.exports = {
  resolver: {
    blacklistRE: blacklist([/dist\/.*/])
  }
};

For React Native >= 0.57, < 0.59

在项目根目录中创建rn-cli.config.js,内容如下:

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

// blacklist is a function that takes an array of regexes and combines
// them with the default blacklist to return a single regex.

module.exports = {
  resolver: {
    blacklistRE: blacklist([/dist\/.*/])
  }
};

For React Native >= 0.52, < 0.57

在项目根目录中创建rn-cli.config.js,内容如下:

const blacklist = require('metro').createBlacklist;

module.exports = {
  getBlacklistRE: function() {
    return blacklist([/dist\/.*/]);
  }
};

For React Native >= 0.46, < 0.52

在项目根目录中创建rn-cli.config.js,内容如下:

const blacklist = require('metro-bundler').createBlacklist;

module.exports = {
  getBlacklistRE: function() {
    return blacklist([/dist\/.*/]);
  }
};

For React Native < 0.46

在项目根目录中创建rn-cli.config.js,内容如下:

const blacklist = require('react-native/packager/blacklist');

module.exports = {
  getBlacklistRE: function() {
    return blacklist([/dist\/.*/]);
  }
};

All versions < 0.59

通过传递--config选项,让CLI命令使用此配置:

react-native run-ios --config=rn-cli.config.js

(配置文件应该由RN>;=0.59自动拾取,因为它被重命名为metro.config.js)

All versions: Note on caching

请注意,您的黑名单项目可能已被打包机包含在缓存中,在这种情况下,第一次使用黑名单运行打包机时,您可能需要将缓存重置为--reset-cache

React-native相关问答推荐

如何在使用嵌套的堆栈导航器在选项卡之间导航后弹出到堆栈顶部?

如何在Reaction Native中将身体分成三部分

react-native useEffect / useFocusEffect / useCallback 没有正确更新

构建 iOS Expo 应用时出现 React-Native xcodebuild 错误 65

React Native 无法下载模板

在 ReactNative 中调用 RCTDeviceEventEmitter.emit 时出错

在 create-react-native-app 元素中突然看到错误Plugin/Preset files are not allowed to export objects, only functions

TypeError:undefined is not an object(判断'navigator.geolocation.requestAuthorization')

如何将 View 定位在 ScrollView 的底部?

React Native:ScrollView 中的 TouchableOpacity onPress 问题

当组件在react-native 中重新呈现时,动态不透明度不会改变

React Native 创建自定义组件/库

如何将捕获的图像与 react-native-camera 一起使用

DeviceInfo 原生模块未正确安装

React.createClass vs. ES6 箭头函数

试图在一个本应是不可变且已被冻结的对象上设置密钥

react-native:webview 高度

Android 是否支持 React Native 的 LayoutAnimation?

React Native 中的 CSS 三角形

无法在react-native中分配给# 的只读属性props