metro icon indicating copy to clipboard operation
metro copied to clipboard

"resolver.extraNodeModules" config doesn't work without "watchFolders"

Open serhiipalash opened this issue 2 years ago • 4 comments

Do you want to request a feature or report a bug?

Bug and feature request

What is the current behavior?

If you want resolver.extraNodeModules to work, you need to add the whole extra modules directory to watchFolders list. In big projects with a lot of shared libraries it causes a bug crashing iOS Simulator opening, if your MacBook is not Pro with 16 Gb RAM.

Error: EMFILE: too many open files, watch at FSEvent.FSWatcher._handle.onchange

What is the expected behavior?

In our project each shared library is a TypeScript projects. We start TS compilation in watch mode for all libraries in parallel with react-native run-ios. Each library has its own .tsbuildinfo file, pointing if the library code was updated or not. We want to add to Metro watchFolders not the whole library directory, but only its .tsbuildinfo. Whenever .tsbuildinfo changes, iOS Simulator will reload.

Unfortunately, configuring metro.config.js like this doesn't work, resulting an error "Can't resolve module ..." from shared library.

It seems that resolver.extraNodeModules requires parent path to be part of watchFolders. We want to make this optional.

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.

const path = require('path')

const extraNodeModules = {
  'ui-kit': path.resolve(__dirname, '../shared/ui-kit/dist')
}

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  resolver: {
    extraNodeModules: new Proxy(extraNodeModules, {
      get: (target, name) => {
        if (name === 'ui-kit') {
          return target[name]
        }

        return path.join(process.cwd(), `node_modules/${name}`)
      },
    }),
  },
  watchFolders: [
    path.resolve(__dirname, '../shared/ui-kit/tsbuildinfo')
  ],
}

Note: We have not the latest version of metro and react-native

"react-native": "0.63.4",
"metro": "0.58",

We are in process of migrating to upcoming RN 0.69.0. Please tell us if this case could be fixed by upgrading to latest RN and Metro?

serhiipalash avatar Jun 17 '22 10:06 serhiipalash

ping @pieterv

serhiipalash avatar Jun 27 '22 05:06 serhiipalash

ping ping @pieterv

serhiipalash avatar Jul 25 '22 15:07 serhiipalash

Have you looked into similar issue already solved like this one? https://github.com/facebook/metro/issues/668#issuecomment-845119616

cortinico avatar Jul 29 '22 16:07 cortinico

Thank you for your help @cortinico !! We will test it on Monday and let you know how it works.

serhiipalash avatar Jul 29 '22 16:07 serhiipalash