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

System limit for number of file watchers reached #7612

Closed
amiramix opened this issue Aug 30, 2019 · 27 comments · Fixed by #8380
Closed

System limit for number of file watchers reached #7612

amiramix opened this issue Aug 30, 2019 · 27 comments · Fixed by #8380

Comments

@amiramix
Copy link

Describe the bug

npm start ends with a cryptic error due to exceeding the amount of files the system can watch:

Starting the development server...

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

Error: ENOSPC: System limit for number of file watchers reached, watch '/home/u/work/some-repo/public'
    at FSWatcher.start (internal/fs/watchers.js:165:26)
    at Object.watch (fs.js:1274:11)
    at createFsWatchInstance (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:37:15)
    at setFsWatchListener (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:80:15)
    at FSWatcher.NodeFsHandler._watchWithNodeFs (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:232:14)
    at FSWatcher.NodeFsHandler._handleDir (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:422:19)
    at FSWatcher.<anonymous> (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:470:19)
    at FSWatcher.<anonymous> (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:475:16)
    at FSReqCallback.oncomplete (fs.js:159:5)
Emitted 'error' event at:
    at FSWatcher._handleError (/home/u/work/some-repo/node_modules/chokidar/index.js:260:10)
    at createFsWatchInstance (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:39:5)
    at setFsWatchListener (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:80:15)
    [... lines matching original stack trace ...]
    at FSReqCallback.oncomplete (fs.js:159:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! some-repo@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the some-repo@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/u/.npm/_logs/2019-08-30T09_50_59_595Z-debug.log

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

exclude blacklist folders directories watch watched

Environment

  System:
    OS: Linux 5.1 Manjaro Linux undefined
    CPU: (16) x64 AMD Ryzen 7 2700X Eight-Core Processor
  Binaries:
    Node: 11.15.0 - /usr/bin/node
    Yarn: Not Found
    npm: 6.10.3 - /usr/bin/npm
  Browsers:
    Chrome: Not Found
    Firefox: 68.0.1
  npmPackages:
    react: ^16.8.1 => 16.8.1 
    react-dom: ^16.8.1 => 16.8.1 
    react-scripts: 2.1.3 => 2.1.3 
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. Create a new app with create-react-app
  2. Add dependencies so that the total amount of files in the project (including node_modules) is larger than the file watch limit (cat /proc/sys/fs/inotify/max_user_watches)

My project doesn't even have many dependencies. Mostly added with npm i apollo-boost graphql react-apollo -S. And it already created three times as many files as the maximum currently allowed by the system to watch. I would expect this problem to be quite common on some systems.

Expected behavior

I am aware of the solutions provided in #2549 and #6068.

Asking users to increase the limit only pushes the problem further without resolving it. And in my opinion is a workaround for a bad design. It also provides a less than ideal user experience and unnecessarily raises the bar for newbie developers.

The npm start script should either:

  1. Not watch all files in the project by default, only project files.

The script can then ask the user to restart the app after updating dependencies and show info to how to enable watching node_modules if they want to recompile automatically after updating dependencies. From my experience it's better to restart the application after dependencies have been updated rather than rely on watch due to problems with watching and compiling source files in linked npm modules (if used of course).

  1. Watch all files but detect when the amount is beyond system limits.

The script can show a warning to the user telling them there is a problem and how to resolve it, then fall back to not watching node_modules until it's resolved. Detecting how many files is in the project is quite quick:

# time find . | wc -l
45601

real    0m0.082s
user    0m0.021s
sys     0m0.064s
  1. Watch all files but there should be another script similar to start that skips watching node_modules.

Providing such a script, say quick-start, could also serve as a quick example how to exclude certain folders from the watcher should the user opt to do that.

In any case the scripts documentation and/or troubleshooting and/or Advanced Configuration guide should mention that the start script will attempt to automatically watch all files in the project, including node_modules. Then it should be documented how to enable or disable (depending on what's the default behaviour) watching specific folders, including how to enable or disable watching node_modules.

Actual behavior

Application doesn't start. Only a cryptic error is shown to the user. For less advanced users it's not possible to figure out what the problem might be without searching the internet and learning all the theory about inotify, kqueue (FreeBSD or Mac) and system limits. They may try various snippets of code available on Stack Overflow or other resources to increase the system limit, which may or may not work due to differences between systems.

@heyimalex
Copy link
Contributor

I agree that we could provide a better user experience.

IMO: Pretty much every software developer bumps their inotify limits once and then never thinks about it again, so this is potentially a lot of work and complexity (changing how we watch isn't necessarily an easy feat) for a rare and easy to fix edge case.

@amiramix
Copy link
Author

I agree in principle but "this is potentially a lot of work and complexity" is a blanket reaction against doing anything and in my opinion is an overstatement. Shouldn't this just be a one line of code in the configuration? C'mon, if excluding a folder from the watcher is going to add "a lot of work and complexity" then something isn't right. Then possibly another line in package.json to run the build without watching that folder?

The watcher implemented in chokidar was actually only watching files loaded during the course of running the application, not all files in node_modules. I could see that I was running above the limit because a certain version of VSCode was trying to watch all the files in the project. Switching to VSCode insider version solved this for me.

However, I actually tried to figure out what it would take to exclude certain folders from watching and it wasn't easy. Stating that the default is for each and every developer to bump their inotify limits is bad. The application should be designed and implemented in such a way that such a simple task should be completed in no time.

@heyimalex
Copy link
Contributor

Stating that the default is for each and every developer to bump their inotify limits is bad.

This is kinda the status quo for development, no? I bumped my inotify limits a long time before create-react-app even existed. Development tools, especially in the node ecosystem, tend to watch a lot of files.

Heh, like I said I think this a good idea! I'm just not personally interested in doing it. CRA is maintained entirely by unpaid volunteers; if the change is "one line of code in the configuration" then please submit a thoughtful PR and join our unpaid ranks :)

@heyimalex
Copy link
Contributor

heyimalex commented Sep 10, 2019

The complexity isn't not watching node_modules fwiw, it's gracefully catching ENOSPC and updating our watch priority accordingly. I think that we could probably provide a better error message, but again if you google "node ENOSPC error" you probably land on a stack overflow post telling you how to fix it.

@amiramix
Copy link
Author

To reiterate, I said that in my opinion it should be a one-line-of-code-type-of-change but it wasn't so easy when I tried to do the change.

In other words, CRA requires to watch all files ever loaded in the app and it doesn't work if the amount of files in the project is larger than the system limit set elsewhere. There is nothing you can do about that in the code. It's a hard requirement and as such should be listed in the documentation. If someone doesn't know how to or can't (e.g. because of permissions) change the limits in their system they should not consider CRA in their project.

This is kinda the status quo for development, no?

No. Watching each file takes CPU and memory. I would rather disable watching node_modules.

I understand the work is done by volunteers and no one really cares. I don't think there is any urgency in fixing this issue, but I would like to see this resulting in an easier configuration to exclude certain folders from the watcher. And if you tell me how to accomplish this I am more than happy to make the necessary changes.

@heyimalex
Copy link
Contributor

I would rather disable watching node_modules.

I just looked at the code and node_modules isn't watched. We're definitely not going to provide configuration to allow you to disable watching files inside of your project tree.

Still open to providing a better error message on ENOSPC. It doesn't look like webpack provides any hooks to catch errors thrown by its watcher, so it might be kinda difficult.

@amiramix
Copy link
Author

I added logs to the chokidar module and I could see files from node_modules being added to the watcher when they were loaded by the app. Otherwise I wouldn't have raised this issue. I will recreate the scenario tomorrow and provide relevant logs.

@amiramix
Copy link
Author

amiramix commented Sep 19, 2019

Sorry for the delay. I added:

console.log('createFsWatchInstance', path); // added line
return fs.watch(path, options, handleEvent);

in nodefs-handler.js in chokidar and disabled all clearConsole calls in react-scripts and WebpackDevServerUtils.js. Then I could see what chokidar is watching. Starting an app (npm start) created with create-react-app watches approximately 1433 files. Only 11 files are watched in the actual app, the rest are watched in node_modules. An excerpt of some of them below:

createFsWatchInstance /home/g/work/frontend-test/node_modules/@apollo/react-components/node_modules/prop-types/lib
createFsWatchInstance /home/g/work/frontend-test/node_modules/@apollo/react-hooks/lib
createFsWatchInstance /home/g/work/frontend-test/node_modules/@babel/runtime/helpers
createFsWatchInstance /home/g/work/frontend-test/node_modules/@material-ui/core/esm/FilledInput
createFsWatchInstance /home/g/work/frontend-test/node_modules/@material-ui/core/esm/Grow
...
createFsWatchInstance /home/g/work/frontend-test/src
Files successfully emitted, waiting for typecheck results...
createFsWatchInstance /home/g/work/frontend-test/src/App.css
createFsWatchInstance /home/g/work/frontend-test/src/App.test.tsx
createFsWatchInstance /home/g/work/frontend-test/src/App.tsx
...
createFsWatchInstance /home/g/work/frontend-test/src/ListView.tsx
createFsWatchInstance /home/g/work/frontend-test/src/index.css
createFsWatchInstance /home/g/work/frontend-test/src/index.tsx
createFsWatchInstance /home/g/work/frontend-test/src/logo.svg
createFsWatchInstance /home/g/work/frontend-test/src/react-app-env.d.ts
createFsWatchInstance /home/g/work/frontend-test/src/serviceWorker.ts
createFsWatchInstance /home/g/work/frontend-test/src/types.tsx
createFsWatchInstance /home/g/work/frontend-test/node_modules/escape-string-regexp/index.js
createFsWatchInstance /home/g/work/frontend-test/node_modules/color-name/test.js
createFsWatchInstance /home/g/work/frontend-test/node_modules/react-dev-utils/FileSizeReporter.js
...
createFsWatchInstance /home/g/work/frontend-test/node_modules/apollo-link/lib/link.d.ts
createFsWatchInstance /home/g/work/frontend-test/node_modules/apollo-link/lib/test-utils.d.ts
createFsWatchInstance /home/g/work/frontend-test/node_modules/json3/lib/json3.js
createFsWatchInstance /home/g/work/frontend-test/node_modules/graphql/jsutils/PromiseOrValue.d.ts
createFsWatchInstance /home/g/work/frontend-test/node_modules/graphql/jsutils/defineToJSON.js.flow
createFsWatchInstance /home/g/work/frontend-test/node_modules/graphql/jsutils/didYouMean.js
createFsWatchInstance /home/g/work/frontend-test/node_modules/graphql/jsutils/inspect.mjs
...

It doesn't watch all files in node_modules but to me it looks like it does watch all files ever loaded or served in the app.

@obendesmond
Copy link

obendesmond commented Dec 13, 2019

I have had this error too. Try

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p

This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

@matheusgnreis
Copy link

Thanks @obendesmond ! Works for me

@ianschmitz
Copy link
Contributor

I think the resolution to this if any is to add a bit of documentation in our troubleshooting page linking or explaining how to increase the limit if running into this error.

@obendesmond
Copy link

My pleasure @matheusgnreis

@Gabkings
Copy link

I have had this error too. Try

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p

This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

This works for me as well

@Rupasharma18
Copy link

I have had this error too. Try

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p

This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

this is working very well. Thank you for help.

@nilam1996
Copy link

I have had this error too. Try
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p
This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

this is working very well. Thank you for help.

Thanks.

@amiramix
Copy link
Author

I think the resolution to this if any is to add a bit of documentation in our troubleshooting page linking or explaining how to increase the limit if running into this error.

How to increase the limit is easy to lookup on the internet (it's even available above). But it's like asking someone to buy a better phone when they complain that the web page is too big to render.

Instead of fixing symptoms, the proper fix would be to fix the root cause - not adding node_modules files to the watcher.

Best fix would be to expose the watcher configuration where one could define to ignore any folders or files of their choosing, not just node_modules.

I tried to do that but I couldn't find where chokidar is taking its configuration from (see my earlier comments). If you could point me to the right direction I would be happy to do the fix.

@Touseef-ahmad
Copy link

Created a project using Web template studio in vscode got same error

@veerbal1
Copy link

Describe the bug

npm start ends with a cryptic error due to exceeding the amount of files the system can watch:

Starting the development server...

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

Error: ENOSPC: System limit for number of file watchers reached, watch '/home/u/work/some-repo/public'
    at FSWatcher.start (internal/fs/watchers.js:165:26)
    at Object.watch (fs.js:1274:11)
    at createFsWatchInstance (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:37:15)
    at setFsWatchListener (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:80:15)
    at FSWatcher.NodeFsHandler._watchWithNodeFs (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:232:14)
    at FSWatcher.NodeFsHandler._handleDir (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:422:19)
    at FSWatcher.<anonymous> (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:470:19)
    at FSWatcher.<anonymous> (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:475:16)
    at FSReqCallback.oncomplete (fs.js:159:5)
Emitted 'error' event at:
    at FSWatcher._handleError (/home/u/work/some-repo/node_modules/chokidar/index.js:260:10)
    at createFsWatchInstance (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:39:5)
    at setFsWatchListener (/home/u/work/some-repo/node_modules/chokidar/lib/nodefs-handler.js:80:15)
    [... lines matching original stack trace ...]
    at FSReqCallback.oncomplete (fs.js:159:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! some-repo@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the some-repo@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/u/.npm/_logs/2019-08-30T09_50_59_595Z-debug.log

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

exclude blacklist folders directories watch watched

Environment

  System:
    OS: Linux 5.1 Manjaro Linux undefined
    CPU: (16) x64 AMD Ryzen 7 2700X Eight-Core Processor
  Binaries:
    Node: 11.15.0 - /usr/bin/node
    Yarn: Not Found
    npm: 6.10.3 - /usr/bin/npm
  Browsers:
    Chrome: Not Found
    Firefox: 68.0.1
  npmPackages:
    react: ^16.8.1 => 16.8.1 
    react-dom: ^16.8.1 => 16.8.1 
    react-scripts: 2.1.3 => 2.1.3 
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. Create a new app with create-react-app
  2. Add dependencies so that the total amount of files in the project (including node_modules) is larger than the file watch limit (cat /proc/sys/fs/inotify/max_user_watches)

My project doesn't even have many dependencies. Mostly added with npm i apollo-boost graphql react-apollo -S. And it already created three times as many files as the maximum currently allowed by the system to watch. I would expect this problem to be quite common on some systems.

Expected behavior

I am aware of the solutions provided in #2549 and #6068.

Asking users to increase the limit only pushes the problem further without resolving it. And in my opinion is a workaround for a bad design. It also provides a less than ideal user experience and unnecessarily raises the bar for newbie developers.

The npm start script should either:

  1. Not watch all files in the project by default, only project files.

The script can then ask the user to restart the app after updating dependencies and show info to how to enable watching node_modules if they want to recompile automatically after updating dependencies. From my experience it's better to restart the application after dependencies have been updated rather than rely on watch due to problems with watching and compiling source files in linked npm modules (if used of course).

  1. Watch all files but detect when the amount is beyond system limits.

The script can show a warning to the user telling them there is a problem and how to resolve it, then fall back to not watching node_modules until it's resolved. Detecting how many files is in the project is quite quick:

# time find . | wc -l
45601

real    0m0.082s
user    0m0.021s
sys     0m0.064s
  1. Watch all files but there should be another script similar to start that skips watching node_modules.

Providing such a script, say quick-start, could also serve as a quick example how to exclude certain folders from the watcher should the user opt to do that.

In any case the scripts documentation and/or troubleshooting and/or Advanced Configuration guide should mention that the start script will attempt to automatically watch all files in the project, including node_modules. Then it should be documented how to enable or disable (depending on what's the default behaviour) watching specific folders, including how to enable or disable watching node_modules.

Actual behavior

Application doesn't start. Only a cryptic error is shown to the user. For less advanced users it's not possible to figure out what the problem might be without searching the internet and learning all the theory about inotify, kqueue (FreeBSD or Mac) and system limits. They may try various snippets of code available on Stack Overflow or other resources to increase the system limit, which may or may not work due to differences between systems.

//This command will definitely solve our issue..
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

huchenme added a commit to huchenme/create-react-app that referenced this issue Jan 27, 2020
@angelocdz
Copy link

delete react node_modules

rm -r node_modules

yarn or npm install

yarn start or npm start

if error occurs use this method again

@MatthewWilkes
Copy link

In case it's useful to have another point of view:

Today is the first time I've tried building a project with React. I have no experience with Node, I'm a Python developer that was interested in learning. It took about 1 hour from a cold start to me finding this bug as the top result when I googled this error message.

It's not at all clear to me why increasing the inotify limit is desirable, as I don't yet know if it's possible to test my code without subscribing to file changes because I'm only a few minutes into using this software.

It's a bit disheartening to find a bug report for the error you're having and have it dismissed as something that's an edge case because it only affects beginners. It looks like a PR has been submitted that will fix this, which is good.

@amiramix
Copy link
Author

amiramix commented Feb 1, 2020

This is a note for anyone landing on this ticket after it has been closed. This issue is caused by create-react-app trying to watch more files than the actual system limit. Currently it adds to the watcher all files loaded by the client, including files located in node_modules.

A proposed fix was to exclude from watcher files from node_modules, or at least make it for the user easier to exclude certain files or folders.

A workaround is of course increasing the system limit, which was by many (incorrectly) reported as the fix, also within this ticket.

I tried to make a change in the code to allow excluding certain folders but it was not easy. So I gave up hoping someone will be able to help. Instead, the ticket was closed.

So, if you plan to use create-react-app in your project, be aware that it will add to the watcher all files used by the client, including those in node_modules. Most of the time you will be fine, or maybe you will need to increase the system limit. But you won't be able to change or configure this behavior.

@tegaguru
Copy link

tegaguru commented Feb 4, 2020

delete react node_modules

rm -r node_modules

yarn or npm install

yarn start or npm start

if error occurs use this method again

Nah fam, apparently it's more than that.

@tegaguru
Copy link

tegaguru commented Feb 4, 2020

I have had this error too. Try

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p

This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Fam!! I think I just might have gone bald due to this bug. I was just all about to go all in on my developing beards - Thanks for saving them!

@obendesmond
Copy link

Welcome @tegaguru

@firmanadiyuda
Copy link

I have had this error too. Try

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p

This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Thank you very much!

@kirbyfern
Copy link

I have had this error too. Try
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p
This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Fam!! I think I just might have gone bald due to this bug. I was just all about to go all in on my developing beards - Thanks for saving them!

I have had this error too. Try
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
and
$ sudo sysctl -p
This document helped me
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Thank you very much!

Thanks folks!

@S-Bowen
Copy link

S-Bowen commented Feb 11, 2020

Hello! Just popping in to support the sentiments of @amiramix - the solution offered isn't possible for me to use as a student working in a linux environment without root permissions. This problem cripples student adoption of react native at my university.

@lock lock bot locked and limited conversation to collaborators Feb 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.