react-native: Error when archiving in XCode: EMFILE

This error has been reported before, but the solution did not help me.

Description

I am seeing this error when archiving from XCode. I have not encountered this issue before and have tried reverting my code:

node /.../node_modules/react-native/cli.js bundle --entry-file index.js --platform ios --dev false --reset-cache --bundle-output ... --assets-dest ...

warning: the transform cache was reset.
                 Welcome to React Native!
                Learn once, write anywhere


events.js:288
      throw er; // Unhandled 'error' event
      ^

Error: EMFILE: too many open files, watch
    at FSEvent.FSWatcher._handle.onchange (internal/fs/watchers.js:127:28)
Emitted 'error' event on NodeWatcher instance at:
    at NodeWatcher.checkedEmitError (/Users/.../node_modules/sane/src/node_watcher.js:143:12)
    at FSWatcher.emit (events.js:311:20)
    at FSEvent.FSWatcher._handle.onchange (internal/fs/watchers.js:133:12) {
  errno: -24,
  syscall: 'watch',
  code: 'EMFILE',
  filename: null
}

I have tried the following steps to fix:

  • Reinstall Watchman (sudo port uninstall watchman, sudo port install watchman, reboot) (this has been the solution on similar issues but has not helped me)
  • Reset Cocoapods (Deintegrate, rm Pods/, reinstall etc)
  • Reset Watchman Cache (watchman watch-del-all)
  • Clean Build Folder in XCode

If I run the bundle command manually from terminal it succeeds, it only seems to have a problem when archiving from XCode.

I also run npx react-native doctor and there is a ✅ next to Watchman

Versions:

Environment: macOS Catalina React Native CLI Version: 4.10.1 Node Version 12.16.1 React Native Version: 0.63.2 Watchman Version: 4.9.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 5
  • Comments: 21 (1 by maintainers)

Most upvoted comments

Interesting development. If I manually run the xcodebuild command the archive succeeds:

xcodebuild -workspace ... -scheme ... archive

Isn’t this basically identical to Product > Archive in XCode? Are there any explanations for this?

Another note: Another much smaller RN app archives successfully in XCode on my machine.

Works for me with same error on latest version of watchman error.

@ekram21 I upgraded watchman, yarn install, and npx pod-install and I am still getting the same error. Am I missing something?

I don’t know your environment and what device you are using but I will list the things to keep in mind:

  • This is for MAC M1 Silicon
  • Latest Versions of Node, React Native being used
  • Latest Versions of Watchman and Brew being used
  • I did not use Yarn. NPM install used to install all node modules and navigated to iOS folder and just used pod install - consult these thread if pod install doesn’t work: https://github.com/CocoaPods/CocoaPods/issues/10349
  • Make duplicate terminals with one running on Rosetta and the other native.
  • Brew should be installed with native terminal, and then watchman after
  • After this you need open Settings - Security Preference and give complete disk access to xcode, watchman, terminal and any other tools being used in your environment.
  • Your project cannot be under desktop or downloads. Make sure it is under /Users/myname/

Also look at this thread: https://github.com/facebook/metro/issues/668

These are the steps I followed to make the project archive in xcode. Note that the project would compile fine in simulator outside of xcode before but the above steps finally got it to archive in xcode where it was failing

EDIT: Also if everything fails then please reset your device, install all latest versions of software needed, follow above steps and then create a clean react-native project using latest watchman and all tools and see if it archives properly. Some people I know have reported success in this method compared to working with existing projects. If this works you can then slowly start figuring out which module is causing problem in existing project.

@ekram21 I upgraded watchman, yarn install, and npx pod-install and I am still getting the same error. Am I missing something?

Interesting development. If I manually run the xcodebuild command the archive succeeds:

xcodebuild -workspace ... -scheme ... archive

Isn’t this basically identical to Product > Archive in XCode? Are there any explanations for this?

Another note: Another much smaller RN app archives successfully in XCode on my machine.