Nodemon Error: “System limit for number of file watchers reached”

Nodemon Error: “System limit for number of file watchers reached”

227

I’m learning GraphQL and am using prisma-binding for GraphQL operations. I’m facing this nodemon error while I’m starting my Node.js server and its giving me the path of schema file which is auto generated by a graphql-cli. What is this error all about?

Error:

Internal watch failed: ENOSPC: System limit for number of file watchers reached, watch ‘/media/rehan-sattar/Development/All projects/GrpahQl/graph-ql-course/graphql-prisma/src/generated

Share
Improve this question

4

  • This is the linux ulimit error see here stackoverflow.com/questions/34588/…

    – Janith

    Dec 26, 2018 at 9:54

  • Tried this! Getting the same error again!

    – Rehan Sattar

    Dec 26, 2018 at 10:27

  • 2

    You are probably watching too many files. Maybe it’s including the nod_modules directory as well?

    – Mikkel

    Dec 27, 2018 at 0:10

  • node_modules are essential because all the packages are there. I’ve tried to kill the previous processes running on the port of my server, it worked for me but I don’t know how long it will take now 😀

    – Rehan Sattar

    Dec 27, 2018 at 5:41

11 Answers
11

Reset to default

515

If you are using Linux, your project is hitting your system’s file watchers limit

To fix this, on your terminal, try:

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

Share
Improve this answer

11

  • 10

    use sysctl --system to reload for more recent systems

    – YLJ

    Mar 6, 2020 at 13:03


  • 30

    is there any other implications that we must know when we do this? I knew this helps solve the issue, I tried it myself. But I am a bit skeptic what possible side effects this fix can cause.

    – Aldee

    May 15, 2020 at 2:45


  • 1

    @Aldee about the technical implications of this change I recommend checking this wiki: github.com/guard/listen/wiki/…

    – Isac Moura

    May 15, 2020 at 4:26


  • 14

    I wouldn’t recommend increasing it so much if you’re not sure how many are in use. Check the number in use with the following find /proc/*/fd -user "$USER" -lname anon_inode:inotify -printf '%hinfo/%fn' 2>/dev/null | xargs cat | grep -c '^inotify'

    – Nick Bull

    Sep 25, 2020 at 12:16


  • 5

    Default value (on Ubuntu 21) was 65535 and setting it to just twice that value (131070) fixed the Node JS issues for me. So according to the principle of minimizing side effects, it is worth trying smaller increments before going all the way to 500k.

    – Dmitriy

    Jul 19, 2021 at 1:41

70

You need to increase the inotify watchers limit for users of your system. You can do this from the command line with:

sudo sysctl -w fs.inotify.max_user_watches=100000

That will persist only until you reboot, though. To make this permanent, add a file named /etc/sysctl.d/10-user-watches.conf with the following contents:

fs.inotify.max_user_watches = 100000

After making the above (or any other) change, you can reload the settings from all sysctl configuration files in /etc with sudo sysctl --system. (On older systems you may need to use sudo sysctl -p instead.)

Share
Improve this answer

7

  • Thank you so much! Worked for me!! But where i have to add this file?

    – Rehan Sattar

    Apr 6, 2019 at 9:11

  • @RehanSattar Create a file /etc/sysctl.d/10-user-watches.conf and in it put fs.inotify.max_user_watches = 100000.

    – cjs

    Apr 6, 2019 at 10:32

  • 1

    Putting this here for completeness echo fs.inotify.max_user_watches=100000 | sudo tee /etc/sysctl.d/10-user-watches.conf && sudo sysctl -p.

    – RedHatter

    Oct 21, 2019 at 21:30


  • 9

    use sysctl --system to reload for more recent systems

    – YLJ

    Mar 6, 2020 at 13:03


  • 1

    @EricBurel It’s a Linux-only answer; I’ve never tried this on Mac. However, the option to reload all sysctl files was incorrect; I’ve fixed it.

    – cjs

    Sep 28, 2022 at 13:22

65

I sometimes get this issue when working with Visual Studio Code on my Ubuntu machine.

In my case the following workaround helps:

Stop the watcher, close Visual Studio Code, start the watcher, and open Visual Studio Code again.

Share
Improve this answer

3

  • That’s right! Because of VSCode. It should be autosave mode.

    – Hùng Ng Vi

    Apr 2, 2019 at 15:18

  • this worked for me; developing with ember-cli

    – reiallenramos

    Jan 20, 2021 at 6:23


  • As w/my focal box this happens intermittently and the VSCode restart is the fix w/out fiddling w/max-_user_watches. (note: When it is working, I typically see watchers in use well below 7k)

    – CNSKnight

    Sep 14, 2021 at 14:43

26

In order to test the changes, I temporary set the parameter with the value 524288.

sysctl -w fs.inotify.max_user_watches=524288

Then I proceed to validate:

npm run serve

And the problem was solved. In order to make it permanent, you should try to add a line in the file “/etc/sysctl.conf” and then restart the sysctl service:

cat /etc/sysctl.conf | tail -n 2
fs.inotify.max_user_watches=524288

sudo systemctl restart systemd-sysctl.service

Share
Improve this answer

1

  • 1

    This hint for test temporarily is priceless. Thank you

    – intmarinoreturn0

    Dec 29, 2020 at 20:27


14

I had the same problem. However, mine was coming from Webpack. Thankfully, they had a great solution on their site:

For some systems, watching many files can result in a lot of CPU or memory usage. It is possible to exclude a huge folder like node_modules using a regular expression:

File webpack.config.js

module.exports = {
  watchOptions: {
    ignored: /node_modules/
  }
};

Share
Improve this answer

0

    5

    This is a problem of inotify (inode notify) in the Linux kernel, so you can resolve it by using this command:

    1. For a temporary solution until rebooting the pc, use the following command
      sudo sysctl -w fs.inotify.max_user_watches=100000
      
    2. A permanent solution: To make this permanent, add a file named /etc/sysctl.d/10-user-watches.conf with the following contents:
      fs.inotify.max_user_watches = 10000
      

    After making the change, reload the settings from all sysctl configuration files in /etc with sudo sysctl -p.

    adasdas
    

    Share
    Improve this answer

      3

      It can be hard to know how much to increase the number of watchers by. So, here’s a utility to double the number of watchers:

      function get_inode_watcher_count() {
        find /proc/*/fd -user "$USER" -lname anon_inode:inotify -printf '%hinfo/%fn' 2>/dev/null | 
        xargs cat | 
        grep -c '^inotify'
      }
      
      function set_inode_watchers() {
        sudo sysctl -w fs.inotify.max_user_watches="$1"
      }
      
      function double_inode_watchers() {
        watcher_count="$(get_inode_watcher_count)"
        set_inode_watchers "$((watcher_count * 2))"
      
        if test "$1" = "-p" || test "$1" = "--persist"; then
          echo "fs.inotify.max_user_watches = $((watcher_count * 2))" > /etc/sysctl.d/10-user-watches.conf
        fi
      }
      
      # Usage
      double_inode_watchers
      # to make the change persistent
      double_inode_watchers --persist
      

      Share
      Improve this answer

        3

        In my case, while I’m doing the nodemon command on the Linux server, I have my Visual Studio Code open (SSH to the server). So based on Juri Sinitson’s answer, I just close Visual Studio Code and run the nodemon command again. And it works.

        My nodemon command:

        nodemon server.js via npm start

        Share
        Improve this answer

        2

        • Worked for me as well. I had basically two VSCode open. Closing the previous one solved the issue.

          – iheathers

          Dec 19, 2021 at 9:05

        • this surprisingly worked!

          – Ahmed J.

          Jan 4, 2022 at 8:43

        1

        I think most answers given here are correct, but using the systemctl command to restart my service solved the problem for me. Check the command below:

        sudo systemctl restart systemd-sysctl.service
        

        Share
        Improve this answer

        1

        • There is nothing in the question about the platform. Can you add the Linux distribution, version, etc. to the answer (but ********************* without ********************* “Edit:”, “Update:”, or similar – the answer should appear as if it was written today)?

          – Peter Mortensen

          Oct 23, 2022 at 19:00


        0

        You should follow answers such as this one:

        cjs’

        Or:

        Isac Moura’s

        And for latest Ubuntu versions, run sudo sysctl --system to read these settings anew.

        However, in my case, my changes to these configuration files were not picked up, because I had already tweaked these settings a while ago… and forgot about it. And I had placed the conflicting configuration file in the wrong place.

        According to man sysctl.d, these settings can be placed in /etc/sysctl.d/*.conf, /run/sysctl.d/*.conf and /usr/lib/sysctl.d/*.conf.

        In my case I had two files:

        /etc/sysctl.d/10-user-watches.conf
        /usr/lib/sysctl.d/30-tracker.conf     <<< Older file, with lower limit
        

        Due to the naming convention, my older file was read last, and took precedence.

        Share
        Improve this answer

          -4

          On Linux, I’ve actually run with sudo.
          sudo npm start

          Share
          Improve this answer

          1

          • 10

            This will often work because root usually has a much higher inotify watch limit than regular users, but it’s a very bad idea to be running things as root when they don’t need to be. See my answer to this question for how to change the user limit.

            – cjs

            Mar 29, 2019 at 6:12



          Your Answer


          Post as a guest

          Required, but never shown


          By clicking тАЬPost Your AnswerтАЭ, you agree to our terms of service, privacy policy and cookie policy

          Not the answer you’re looking for? Browse other questions tagged

          or ask your own question.

          Leave a Reply

          Your email address will not be published. Required fields are marked *