Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RN - Error: jest-haste-map: @providesModule naming collision #265

Open
jaimecbernardo opened this issue Sep 25, 2018 · 33 comments
Open

RN - Error: jest-haste-map: @providesModule naming collision #265

jaimecbernardo opened this issue Sep 25, 2018 · 33 comments

Comments

@jaimecbernardo
Copy link

Reporting this error here as well, as suggested in the corresponding react-native issue: facebook/react-native#21242 (comment)

It's related to react-native's usage of metro, where a plugin might need to instruct metro to ignore some paths in the react-native project. Discussion here might helpful, as there might already be a way to achieve this.

Here's the original issue:

Environment

  React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.6
      Memory: 1.69 GB / 8.00 GB
      Shell: 4.4.12 - /usr/local/bin/bash
    Binaries:
      Node: 8.9.2 - /usr/local/bin/node
      npm: 5.5.1 - /usr/local/bin/npm
      Watchman: 4.7.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
      Android SDK:
        Build Tools: 23.0.1, 25.0.0, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.1, 27.0.3, 28.0.0, 28.0.1
        API Levels: 23, 24, 25, 26, 27
    IDEs:
      Android Studio: 3.0 AI-171.4443003
      Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.5.0 => 16.5.0 
      react-native: 0.57.0 => 0.57.0 
    npmGlobalPackages:
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7

Description

The nodejs-mobile-react-native plugin runs a nodejs project in a react-native application. It creates a nodejs-project folder in the react-native's project root folder, which will be copied in the build process to be included in the mobile application assets, which means the Bundler will detect duplicate modules.

Before react-native version 0.56, this duplication caused a warning while the bundler was starting, but the mobile application was still able to function and get the react-native files from the bundler. In and after version 0.56, the bundler will now throw an error when the mobile application tries to get the bundle from the development machine:
error: bundling failed: Error: jest-haste-map: @providesModule naming collision:

Before react-native version 0.56, it was possible to use the plugin without creating a rn-cli.config.js file in the react-native project, as this was only a warning, but now creating a rn-cli.config.js file in the main project with a blacklist for the Metro bundler is mandatory for using the plugin: https://github.com/janeasystems/nodejs-mobile-react-native/tree/ed727edea17e8a9e1a85cef3413becc83b8a0328#duplicate-module-name

Since this is now an error that won't allow the application to work, is there something that can be done from inside the plugin to have the Metro bundler ignore some paths?

Thank you, in advance.

Reproducible Demo

Before react-native version 0.56, the application is able to run successfully.

react-native init DuplicateModulesWarning --version 0.55.4
cd DuplicateModulesWarning
npm install nodejs-mobile-react-native
mv nodejs-assets/nodejs-project/sample-package.json nodejs-assets/nodejs-project/package.json
mv nodejs-assets/nodejs-project/sample-main.js nodejs-assets/nodejs-project/main.js
react-native link
react-native run-android

The react packager throws warnings but the application can still run and get the bundle from the development machine:

Loading dependency graph, done.
jest-haste-map: @providesModule naming collision:
  Duplicate module name: sample-node-project

In and after react-native version 0.56, the application will fail to run due to the bundler failing:

react-native init DuplicateModulesError --version 0.56.0
cd DuplicateModulesError
npm install nodejs-mobile-react-native
mv nodejs-assets/nodejs-project/sample-package.json nodejs-assets/nodejs-project/package.json
mv nodejs-assets/nodejs-project/sample-main.js nodejs-assets/nodejs-project/main.js
react-native link
react-native run-android

The same warning is still thrown but now there is an additional error when the application running in the device tries to get the bundle from the development machine:

error: bundling failed: Error: jest-haste-map: @providesModule naming collision:
  Duplicate module name: sample-node-project
  Paths: /Users/username/temp-projects/DuplicateModulesError/nodejs-assets/nodejs-project/package.json collides with /Users/username/temp-projects/DuplicateModulesError/android/build/nodejs-assets/nodejs-project/package.json

This error is caused by a @providesModule declaration with the same name across two different files.
    at setModule (/Users/username/temp-projects/DuplicateModulesError/node_modules/jest-haste-map/build/index.js:446:17)
    at workerReply (/Users/username/temp-projects/DuplicateModulesError/node_modules/jest-haste-map/build/index.js:496:9)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
 BUNDLE  [android, dev] ../../index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1), failed.

Sometimes this error won't occur in the first run, so there's probably a timing issue here. If it doesn't occur in the first run, close the Metro Bundler and run react-native run-android again.

@rafeca
Copy link
Contributor

rafeca commented Sep 25, 2018

Hi! Thanks for reporting!

The metro resolution logic (which is powered by jest-haste-map) treats any folder with a package.json as a global module, so in this case it's mandatory to have a blacklistRE option param to tell Metro to ignore that folder.

This should be done in the default Metro config for RN apps (which lives here), IMHO it should be fine to always add the build folders in the blacklist of Metro, since nothing should be resolved from there.

@jaimecbernardo
Copy link
Author

Hi! Thank you for the answer.
That's the current workaround, which is to instruct the user to create the configuration file: https://github.com/janeasystems/nodejs-mobile-react-native/tree/ed727edea17e8a9e1a85cef3413becc83b8a0328#duplicate-module-name

Is there a way this could be done from inside the plugin code?

@rafeca
Copy link
Contributor

rafeca commented Sep 25, 2018

Unfortunately there's no way for plugins to modify the config object, what you can do is implement some util method in nodejs-mobile-react that can be used by users to automatically update the config adding the correct blacklist

@jose2007kj
Copy link

jose2007kj commented Oct 25, 2018

i have added rn-cli.config.js file to my react native project root...... with he contents mentioned here but still i am getting he same error like

github
sir @jaimecbernardo @rafeca ,where i might be going wrong
react-native version 0.57.0

@jose2007kj
Copy link

solved it by changing the conflict making libraries to old version

@jamalx31
Copy link

jamalx31 commented Dec 12, 2018

solved by adding rn-cli.config.js

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

module.exports = {
    resolver: {
        blacklistRE: blacklist([
            /node_modules\/.*\/node_modules\/react-native\/.*/,
        ])
    },
};

@borgogelli
Copy link

borgogelli commented Jan 12, 2019

Why Metro doesn't honor the following config in the package.json ?

  "jest": {
    "preset": "jest-expo",
    "modulePathIgnorePatterns": [
        ".yarn", ".npm"
    ]
  },

(see the modulePathIgnorePatterns option)

@nvmanh
Copy link

nvmanh commented Feb 15, 2019

@jaimecbernardo confirmed your solution works by adding:

const blacklist = require('metro-config/src/defaults/blacklist');
module.exports = {
  resolver:{
    blacklistRE: blacklist([
      /nodejs-assets\/.*/,
      /android\/.*/,
      /ios\/.*/
    ])
  },
};

@RavichandraBadeka
Copy link

RavichandraBadeka commented Mar 8, 2019

Can anyone tell me where to use this below code

const blacklist = require('metro-config/src/defaults/blacklist');
module.exports = {
resolver:{
blacklistRE: blacklist([
/nodejs-assets/./,
/android/.
/,
/ios/.*/
])
},
};

@antgel
Copy link

antgel commented Mar 12, 2019

@RavichandraBadeka Please format your code with Markdown.

@antgel
Copy link

antgel commented Mar 12, 2019

Workaround not working for me when trying to refactor part of an Expo app into a common module. Issue at expo/expo#3713 if anyone has any clues if it's my error or a deeper bug.

@thg303
Copy link

thg303 commented Mar 31, 2019

@RavichandraBadeka simply put the code in a file, name it rn-cli.config.js and put it in your project's root directory (next to package.json). that's all

@shreyasb07
Copy link

shreyasb07 commented Apr 3, 2019

@jamalx31 & @thg303, I added the rn-cli.config.js file in the root directory of my project, restarted the terminal and XCode, but I still get the same error on Metro.

Could there possibly be something that I am missing here?
Screen Shot 2019-04-02 at 5 59 33 PM

@thg303
Copy link

thg303 commented Apr 3, 2019

@shreyasb07 I see you have metro.config.js there too. I think in newer versions metro uses that instead of rn-cli.config.js, so try putting content there too. if that worked, you can remove rn-cli.config.js.

@nvest3
Copy link

nvest3 commented Apr 3, 2019

@shreyasb07 I see you have metro.config.js there too. I think in newer versions metro uses that instead of rn-cli.config.js, so try putting content there too. if that worked, you can remove rn-cli.config.js.

@thg303 thanks a lot. you saved my day

@shreyasb07
Copy link

@thg303 thank you so much, that works! I really appreciate it!

@jaimecbernardo
Copy link
Author

Thank you @thg303 .
The files that are looked for seem to be defined here :

'metro.config.js',
'metro.config.json',
'package.json',
'rn-cli.config.js',

@Kamalnrf
Copy link

I'm getting this error after adding blacklist.

Error: Unable to resolve module `./index`

@tbarbugli
Copy link

I get this error by following the official tutorial from Facebook React Native

@Sharcoux
Copy link

Sharcoux commented Jun 3, 2019

Ok, I got the solution after 3 days of trials with everything I could find on the internet.

Add this in your podfile:

  pod 'React', :path => '../node_modules/react-native'
  pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'

Then in your ios folder, do:

rm -Rf Pods && pod install

That last line might seem obvious for most of you but it was not for me as I usually let react-native handle everything that happens at ios or android level.
The problem should now be solved.

@mymai91
Copy link

mymai91 commented Jun 4, 2019

I have same error. I tried remove node module many time but didn't solve problem.

And then I open 1 tab run npm start -- --reset-cache => this tab should be run first.
(1 more thing quite weird I ran with yarn start -- --reset-cache BUT yarn not help to solve this problem)

open another tab run react-native run-ios

@ShashankMalviya1994
Copy link

ShashankMalviya1994 commented Jun 15, 2019

Only this works for me, I don't know how many other issues i am going to face for this.
`const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
resolver:{
blacklistRE: blacklist([
/node_modules\/.*\/node_modules\/react-native\/.*/,
])
},
};`

@n-k-d
Copy link

n-k-d commented Jun 27, 2019

not working for me anything.. :(

@lupelius
Copy link

This is my metro.config.js (I know I don't need both resolver and transformer.getBlacklistRE before you say it, I wanted to show I tried a couple things) and I also tried mymai91's suggestion to reset cache on one tab and run ios on a second one, nothing is working... I've been on this stuck for two days, every time you install a new package and link, react native stops working, it's so frustrating

`/**

module.exports = {
resolver:{
blacklistRE: blacklist([
/node_modules/./node_modules/react-native/./,
])
},
transformer: {
getBlacklistRE: () => {
blacklist([
/node_modules/./node_modules/react-native/./,
/nodejs-assets/./,
/android/.
/,
/ios/./,
/node_modules\.
\node_modules\react-native\./,
/nodejs-assets\.
/,
/android\./,
/ios\.
/
])
},
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};`

@lupelius
Copy link

OK About to board a plane so can't find out which bit worked but following metro config works finally, wanted to share:

`/**

module.exports = {
resolver:{
blacklistRE: blacklist([
/react-native/local-cli/core/fixtures./,
/node_modules/.
/node_modules/react-native/./,
/nodejs-assets/.
/,
/android/./,
/ios/.
/,
/node_modules\.\node_modules\react-native\./,
/nodejs-assets\./,
/android\.
/,
/ios\./
])
},
getBlacklistRE () {
return blacklist([
/react-native/local-cli/core/fixtures.
/,
/node_modules/./node_modules/react-native/./,
/nodejs-assets/./,
/android/.
/,
/ios/./,
/node_modules\.
\node_modules\react-native\./,
/nodejs-assets\.
/,
/android\./,
/ios\.
/
])
},
transformer: {
getBlacklistRE () {
blacklist([
/node_modules/./node_modules/react-native/./,
/nodejs-assets/./,
/android/.
/,
/ios/./,
/node_modules\.
\node_modules\react-native\./,
/nodejs-assets\.
/,
/android\./,
/ios\.
/
])
},
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};`

@BarglanceDev
Copy link

@lupelius Awesome! What worked for you? Im on this now

@watchtower314
Copy link

not working for me anything.. :(

did you manage to find a fix?

@kurt-abela
Copy link

I'm getting this error after adding blacklist.

Error: Unable to resolve module `./index`

Exactly same error, did you manage to fix it..?

@akhilvc10
Copy link

I'm getting this error after adding blacklist.

Error: Unable to resolve module `./index`

Same error. did you fix it ?

@ghost
Copy link

ghost commented Aug 23, 2019

@shreyasb07 I see you have metro.config.js there too. I think in newer versions metro uses that instead of rn-cli.config.js, so try putting content there too. if that worked, you can remove rn-cli.config.js.

you saved my day 💯

@ldco2016
Copy link

Okay, I am confused, I have this metro.config.js:

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
};

and it was not because of this error, I just recently started getting this error and then I added rn-cli.config.js:

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

module.exports = {
  resolver: {
    blacklistRE: blacklist([/node_modules\/.*\/node_modules\/react-native\/.*/])
  }
};

So what am I supposed to be doing? Merging both of these configurations?

This is really doing a number when I try to create an .ipa in Xcode with a couple of my targets.

@jaimecbernardo
Copy link
Author

Hi @ldco2016 ,
If there's a metro.config.js file it'll take precedence over rn-cli.config.js, so you need to add your definitions to metro.config.js.

@whalemare
Copy link

whalemare commented Feb 20, 2022

For someone from our days, blacklist was be renamed to blocklist, and resolver block looks like:

const exclusionList = require('metro-config/src/defaults/exclusionList');
...
  resolver: {
          blockList: exclusionList([
            /dist\/libs\/.*/,
          ]),
  }
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests