74

I try to run a react-native-cli project using react-native start, and the metro bundler fails, saying the following:

events.js 187
throw er; //Unhandled 'error' event

Error: EMFILE: too many open files, watch

Emitted 'error' event on NodeWatcher isntance at:
     at NodeWatcher: checkedEmitError (.../react-native-project/node_modules/sane/src/node_watcher.js:143:12)
     at FSWatcher.emit (events.js:210:5)
     at FSEvent.FSWatcher:_handle onchange (internal/fs/watchers.js:129:12) {
   errno: -24,
   syscall: 'watch',
   code : 'EMFILE',
   filename: null
)
Process terminated. Press <enter> to close the window
Amir
  • 1,422
  • 1
  • 15
  • 28

22 Answers22

99

Just close all other files and try again. If you are running on android make sure to run cd android and ./gradlew clean before react-native run-android. If this doesn't fix, there might be problem with your file watching service. Try this

$ brew update
$ brew install watchman
Romit Kumar
  • 2,442
  • 6
  • 21
  • 34
  • 2
    I'm running on ios. and I don't have any files running that I can see – Amir Nov 02 '19 at 21:20
  • This issue is not specific to React. I hit upon this issue when running `Jest`. Looks like this is an `iOS` issue. When `watchman` is installed, the tools seem to use it for watching file changes. Watchman can watch large number of files. Therefore, installing it most probably would solve the issue. – HelloWorld101 Apr 09 '20 at 13:36
  • 1
    this issue raised for me, because I have two react-native projects running in different directories with different versions. `brew upgrade watchman` helped me – Radha Manohar Apr 10 '20 at 17:24
  • 2
    In Catalina sometimes watchman does not have permission to access Document folder where the projects are, so it gives this error, make sure you have required permission given else try removing and adding watchman again. – Divyanshu Negi May 28 '20 at 05:14
  • 4
    Running `brew reinstall watchman` finally fixed the error for me (had to do `xcode-select --install` for it to work too). It asked for permission to access Document folder after I did that. – Dror Bar Mar 02 '21 at 08:41
  • 2
    Worked for me, I am using Bigsur with Mac M1 and I upgraded from nodejs 16 to 17 (rather composer did) I had this issue and installing watchman with `arch -arm64 brew install watchman` helped me , I'm commenting incase anyone ran into the same issue – TGod-Ajayi Dec 08 '21 at 05:29
46

I'm using a Mac and got the EMFile error. It was resolved when I installed the file watcher

brew install watchman

Kyle Challis
  • 981
  • 2
  • 12
  • 28
Kaushik
  • 469
  • 4
  • 2
  • This worked for me. I'm using Macbook pro M1 chip MacOS Big Sur. Node version is 14.16.0. I was having the issue while working with React native project created using Expo. `brew install watchman` worked for me. Thanks – Gulam Hussain Sep 02 '21 at 02:58
  • 2
    This is what worked for me : 1 - delete node modules 2 - brew reinstall watchman 3 - install node modules 4 - then try building the project – Manju JK Nov 23 '21 at 05:55
27

In my case, upgrading watchman did not help.

This problem started to happen after having updated Node.js.

Thanks to nvm, I came back to node v8.11.3 (I updated to v12.0.4) and this issue disappeared.

That might help some of you having the same problem...

Żabojad
  • 2,946
  • 2
  • 32
  • 39
  • 3
    This was indeed what was happening for me as well. I had just upgraded from Node `10.16.2` to the latest LTS (at the time of this writing `12.16.1`) and ran into this. I bumped the `.nvmrc` back down to `10.16.2` and it worked like a charm. – John T Feb 29 '20 at 18:44
  • Same here. Darn Node 12. – phatmann Jul 21 '20 at 00:17
  • I believe this was the fix for me. downgrading Node fixed the issue for me – Amir Oct 02 '20 at 17:13
  • Node v14.17.4 has the same error for me. @MarceloSouza, could you please let me know your exact node version? thanks in advance. – Bayu Aug 06 '21 at 04:08
  • 1
    reinstall `node_modules` after changing the node version if you previously installed them with the broken version – geg Aug 11 '21 at 16:40
  • @John T Did you'll get any solution to fix above too many open files issue? Even we are facing it on react native 0.63.3 and ventura mac os. Is it because of incompatibility between react native and newest version of ventura macos? – Vineeta Jul 27 '23 at 08:59
16

For my case, I think this error occurs due to node upgrade.

Delete node_modules folder and run npm install solved the issue.

luke77
  • 2,255
  • 2
  • 18
  • 30
13

I needed to install watchman with brew as others have done here, however I also needed to update the React Native Xcode build script with this line: export PATH=/opt/homebrew/bin:$PATH so xCode could find watchman in my M1 MacBook Pro.

React Native Xcode build script location: ./node_modules/react-native/scripts/react-native-xcode.sh

Oscar
  • 376
  • 4
  • 11
12

I'm using a MAC and Node v12.13.1. I upgraded watchman and the issue got resolved.

watchman 4.9.0_2 -> 4.9.0_4

Ashish Vyas
  • 121
  • 1
  • 2
10

I had this issue after upgrading my node version. Downgrading was not an option in my case.

brew install watchman did not fix the issue, as watchman was already installed on my machine. brew upgrade watchman did the trick for me.

Mike Hamilton
  • 1,519
  • 16
  • 24
8

TLTR: brew install watchman solved it on a Macbook.

I just got a new Macbook and was having the same issue with a React Native project. On Windows the project was working perfectly but in Mac, after a few seconds of running the project with npm start, it crashed with this error:

Error: EMFILE: too many open files, watch
    at FSEvent.FSWatcher._handle.onchange (internal/fs/watchers.js:178:28)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ start: `expo start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

PS: npm install didn't work. It has to be with brew install.

  • How does this solve the issue? Is it related to the Node version? – ryanwebjackson Jun 07 '21 at 01:06
  • I have a Macbook Air M1 with node 14.17.0, and for some reason I was having the above mentioned error when trying to run the expo app. Installing Watchman fixed the error. I guess that maybe the watchman version that came with Node was older than the required. – Lorenzo Castillo Jun 07 '21 at 23:13
4

No matter what I did, I couldn't get this to work. What ended up working for me is using the following command

$ sudo sysctl -w kern.maxfiles=10485760
$ sudo sysctl -w kern.maxfilesperproc=1048576

Found at: https://facebook.github.io/watchman/docs/install.html#mac-os-file-descriptor-limits

I have a M1 Pro. Hope this is helpful to someone.

  • Thank you for posting this! it solved my issue. Even though the documentation mentions those are only applicable on macOS 10.6 and **earlier**, they worked in my case (macOS 13.0). – Abdulazeez Salam Mar 27 '23 at 21:53
2

For me, a simple upgrade watchman didn't help, and neither did changing my Node version.

I was building on an M1 Mac using XCode 12.5 and had to:

  • Install watchman on using an x86_64 instance of homebrew
  • Run the metro server in a Rosetta terminal
  • Run XCode without Rosetta

If this is the first time you're installing a package using the x86_64 instance of homebrew remember to add it to your PATH.

Redseb
  • 199
  • 10
  • Wow, that fixed it for me too I would not have thought to run Terminal in Rosetta mode. – Oscar Nov 30 '21 at 23:56
2

Tried everything but nothing changed.

After 2 days ->

Just killall node and

restart your computer and it works for me.

A. Berk
  • 127
  • 1
  • 3
1

I had a bunch of these errors along with others like Could not find "Podfile.lock", etc. after installing PHP Storm.

I did the following to correct the errors based off a combination of StackOverflow searches:

  1. Download, install, and agree to the XCode license
  2. Install brew via the terminal window at brew.sh
  3. Install node
  4. Followed the instructions in the iOS terminal window in PHPStorm

Now working!

FontFamily
  • 355
  • 4
  • 13
1

I had watchman installed via macports. However the path to watchman was unknown to to the "Bundle React Native code and images" Build Phase. Adding

export PATH=/opt/local/bin:$PATH

to the beginning of the script fixed the issue for me

Micha
  • 61
  • 6
1

Try "brew install watchman" command,

if "brew" not install then follow below steps

Follow this steps, This is worked for me.

Vishal Gadhiya
  • 450
  • 1
  • 6
  • 21
0

The following steps worked for me on MacOS:

  1. Updating/Installing Homebrew from official site https://brew.sh/
  2. Installing Watchman: brew install watchman
0

In my case I got this error after switching dev machines from Linux to OS X. None of the above worked for me until I ran brew upgrade watchman.

Mr. Robot
  • 1,334
  • 6
  • 27
  • 79
0

Tried with couple of node versions but giving same error.

brew update watchman

helped to get rid of this error.

pjoshi
  • 249
  • 3
  • 7
0

In my case, the icu4c version was wrong. I reinstall icu4c then it's worked. Here is the command which I used:

 brew reinstall icu4c
Heo Đất Hades
  • 1,573
  • 18
  • 14
0

Works inside docker: ulimit -n 2048

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 26 '22 at 03:10
0

Check that ~/Library and ~/Library/LaunchAgents are owned by your user. In my case, ~/Library/LaunchAgents was owned by root which was preventing watchman from writing to ~/Library/LaunchAgents/com.github.facebook.watchman.plist. This was not immediately obvious as watchman was being used indirectly through a local dev server which suppressed the helpful error. I ultimately identified the issue by manually attempting to watch my project.

    # Attempt manual watch of project, logs helpful error that was suppressed by local dev server
    watchman watch-project /path/to/your/project
    # Permissions error
    [cli] Failed to open /Users/<your_user>/Library/LaunchAgents/com.github.facebook.watchman.plist for write: Permission denied

    # check Library ownership
    ls -ld ~/Library
    # root owns Library
    drwx------@ 87 root  staff  2784 Jan 23 09:42 /Users/<your_user>/Library
    # fix Library ownership
    sudo chown $(id -u) ~/Library

    # check LaunchAgents ownership
    ls -ld ~/Library/LaunchAgents
    # root owns LaunchAgents
    drwxr-xr-x@ 4 root  staff  128 Jan 26 09:05 /Users/<your_user>/Library/LaunchAgents
    # fix LaunchAgents ownership
    sudo chown $(id -u) ~/Library/LaunchAgents

This fix is also explained in this stackoverflow answer

Randy
  • 1
  • 2
0

In my case that was both node_modules and node itself, so I downgraded node to 16 and reinstalled node_modules and it worked.

0

On my laptop, watchman was located at /opt/homebrew/bin/watchman. Adding a symlink to /usr/local/bin/watchman worked for me:

sudo ln /opt/homebrew/bin/watchman /usr/local/bin/watchman
adriank
  • 143
  • 11