facebook / metro

πŸš‡ The JavaScript bundler for React Native
https://metrobundler.dev
MIT License
5.18k stars 614 forks source link

Bundler looks in directories besides node_modules for node_modules #97

Open aaroncraig10e opened 6 years ago

aaroncraig10e commented 6 years ago
yarn
mv node_modules xxx
yarn
yarn react-native start
# load app in a simulator
This warning is caused by a @providesModule declaration with the same name across two different files.
jest-haste-map: @providesModule naming collision:
  Duplicate module name: react-animated
  Paths: /path/to/my/project/node_modules/react-native/Libraries/Animated/release/package.json collides with /path/to/my/project/xxx/react-native/Libraries/Animated/release/package.json

error: bundling failed: ambiguous resolution: module `/path/to/my/project/index.js` tries to require `react-native`, but there are several files providing this module. You can delete or fix them:

This seems to be unexpected behavior. If the convention is for node modules to live under node_modules, why would Metro Bundler take it upon itself to look anywhere else?

ulfgebhardt commented 6 years ago

I use React-Native in combination with Electron to build iOS, Android, Desktop and Webversions of my Application. Electron generates a folder node_modules under ressource in its output. This folder is required to run the Desktop Application. Once the Desktop Version is compiled, node fails to build the iOS and Android, aswell as the Web Application since it will find two node_modules folders, containing duplicate modules.

This results in following errors:

Scanning folders for symlinks in D:\sources\demokratie-live\democracy-client\node_modules (66ms)
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚  Running Metro Bundler on port 8081.                                       β”‚
 β”‚                                                                            β”‚
 β”‚  Keep Metro Bundler running while developing on any JS projects. Feel      β”‚
 β”‚  free to close this tab and run your own Metro Bundler  instance if you    β”‚
 β”‚  prefer.                                                                   β”‚
 β”‚                                                                            β”‚
 β”‚  https://github.com/facebook/react-native                                  β”‚
 β”‚                                                                            β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Looking for JS files in
   D:\sources\demokratie-live\democracy-client

Metro Bundler ready.

Loading dependency graph, done.
Bundling `index.js`  [development, non-minified]  0.0% (0/1), failed.
error: bundling failed: ambiguous resolution: module `D:\sources\demokratie-live\democracy-client\index.js` tries to require `react-native`, but there are several files providing this module. You can delete or fix them:

  * `D:\sources\demokratie-live\democracy-client\dist\win-unpacked\resources\app.asar.unpacked\node_modules\react-native\package.json`
  * `D:\sources\demokratie-live\democracy-client\node_modules\react-native\package.json`

The question is: How do I tell metro bundler to ignore a certain node_modules folder or specify which one it should use?

Rel: https://github.com/demokratie-live/democracy-client/tree/desktop Rel: https://github.com/nodejs/help/issues/1011

Grüße Ulf

<3

ulfgebhardt commented 6 years ago

Bump - this issue prevents us from deploying Desktop Versions of our Software </3

Grüße Ulf

<3

ManAnRuck commented 6 years ago

+1

OisinOKeeffe commented 6 years ago

I am also having this isssue, albeit for different reasons. I am using a monorepo approach for my current project and if I want to have multiple projects with different versions of react-native I have to remove and the node_modules folder from one project so as to avoid getting this error.

Being able to specify which path to ignore would allow me to circumvent this issue and have two react-native projects within the same monorepo using different versions of react.

OisinOKeeffe commented 6 years ago

I was able to fix this in my case by adding a rn-cli.config.js file to the root of my monorepo with the following content:

const metroBundler = require('metro-bundler');

module.exports = { getBlacklistRE: function() { return metroBundler.createBlacklist([/NAME_OF_FOLDER_TO_EXCLUDE/.*/]); } };

When I switch from project to project I can just update the excluded folder and I am good to go. Two different versions of React-Native in the same monorepo.

flybayer commented 6 years ago

My workround:

rn-cli.config.js

const metro = require("metro")

module.exports = {
  getBlacklistRE: function() {
    return metro.createBlacklist([/dist\/mac\/.*/, /dist\/win-unpacked\/.*/])
  },
}