Fixing Node.js ENOSPC (file watchers limit) on a Linux dev server
On Linux, the ENOSPC: System limit for number of file watchers reached error that appears when you start a dev server such as Vite means that the number of files that can be watched through inotify, the Linux mechanism for notifying file changes, has reached the kernel limit fs.inotify.max_user_watches. If you save a file and the page does not update (hot reload does not work), and this line keeps appearing in the terminal, raise the limit or watch fewer files and the problem goes away.
I tested this in a test environment by deliberately lowering the limit to trigger the error. I confirmed the error message, and that both fixes made the error disappear and file changes reach the browser.
The test environment was Ubuntu 24.04 (kernel 6.18), Node.js 22.23.4, and Vite 8.3.8. I did not test dev servers other than Vite, such as webpack or Next.js, nor did I test running inside Docker containers or WSL, or whether the settings persist after a reboot.
The error the dev server printed
For the test, I made a project with index.html and src/main.js, plus 400 JavaScript files under src/ and 500 JSON files under fixtures/. src/main.js loads the value in src/dir40/m1.js and shows it in the page heading.
The limit in the test environment was 128415. That would not cause an error as is, so I lowered the limit to 100 and then started the Vite dev server.
$ sudo sysctl fs.inotify.max_user_watches=100
fs.inotify.max_user_watches = 100
$ npx vite --port 5173
VITE v8.3.8 ready in 159 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
file watcher error: ENOSPC: System limit for number of file watchers reached, watch '/path/to/app/fixtures/set1/case40.json'
file watcher error: ENOSPC: System limit for number of file watchers reached, watch '/path/to/app/fixtures/set1/case41.json'
file watcher error: ENOSPC: System limit for number of file watchers reached, watch '/path/to/app/fixtures/set1/case42.json'
(以下、同じ形の行が続く)
I replaced the test working directory in the watch '…' paths with /path/to/app. In this run, 865 lines of the same form were printed.
The dev server kept running and the page opened. However, src/dir40/m1.js was among the files that could not be watched. With the page open in the browser, I changed the value in this file from 1 to 100, but the heading still showed v1 = 1 after 3 seconds. The error lines just scroll past at startup, so you only notice the problem when the page does not change after you save.
What it looks like with Node.js fs.watch
Vite watches for file changes with a library called chokidar (server.watch in the official docs). According to the chokidar 3.6.0 README, on Linux chokidar uses Node.js's fs.watch. As stated in the “Availability” section of the fs.watch docs, on Linux it receives file change events through inotify.
With the limit still at 100, I used the following hold.mjs to watch files in fixtures/ until the limit was used up.
// Watch files in fixtures until the limit is reached, so that all watches are used up
import { readdirSync, watch } from 'node:fs';
for (const d of readdirSync('fixtures')) {
for (const f of readdirSync(`fixtures/${d}`)) {
try { watch(`fixtures/${d}/${f}`); } catch {}
}
}
setTimeout(() => process.exit(0), 3000);
In that state, I ran the following watch-one.mjs, which only watches one file with fs.watch. It ended with an error. (I replaced part of the path.)
// Watch one file with Node.js fs.watch
import { watch } from 'node:fs';
watch(process.argv[2], () => console.log('changed'));
console.log('watching', process.argv[2]);
$ node hold.mjs & sleep 1; timeout 2 node watch-one.mjs src/main.js; wait
node:internal/fs/watchers:254
const error = new UVException({
^
Error: ENOSPC: System limit for number of file watchers reached, watch 'src/main.js'
at FSWatcher.<computed> (node:internal/fs/watchers:254:19)
at watch (node:fs:2554:36)
at file:///path/to/app/watch-one.mjs:3:1
at ModuleJob.run (node:internal/modules/esm/module_job:343:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:681:26)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5) {
errno: -28,
syscall: 'watch',
code: 'ENOSPC',
path: 'src/main.js',
filename: 'src/main.js'
}
Node.js v22.23.4
ENOSPC (errno: -28) is an error code that originally means "No space left on device." In the Linux 6.18 source (fs/notify/inotify/inotify_user.c), inotify returns this code when it cannot increase the number of watches for a user. The second half of the message, System limit for number of file watchers reached, also means that the limit on the number of watchable files has been reached. In the Vite dev server, the same message appeared after file watcher error:, and the dev server kept running.
Checking the current usage and the limit
You can check the limit with sysctl or /proc/sys/fs/inotify/max_user_watches.
$ sysctl fs.inotify.max_user_watches fs.inotify.max_user_instances
fs.inotify.max_user_watches = 128415
fs.inotify.max_user_instances = 128
This limit applies per user. In the Linux 6.18 source mentioned above, the default is calculated from 1% of the machine's memory and kept between 8192 and 1048576. Machines with less memory get a smaller default.
You can see how many watches each process uses by looking at /proc/<pid>/fdinfo/. The kernel documentation for /proc includes an example of the fdinfo for an inotify file descriptor, which has one line in the form inotify wd:… per watch. The following count-watches.sh counts these lines and shows the processes with the most watches first.
# Show the processes that use inotify watches and their counts, highest first
for fd in /proc/[0-9]*/fdinfo/*; do
n=$(grep -c '^inotify' "$fd" 2>/dev/null) || continue
[ "$n" -gt 0 ] || continue
pid=${fd#/proc/}; pid=${pid%%/*}
echo "$n $pid $(tr '\0' ' ' < /proc/$pid/cmdline | cut -c1-60)"
done | sort -rn | head -5
After raising the limit enough and starting Vite in the test project, the first line of the script's output showed that Vite alone was using 958 watches. The columns are, from the left, the count, the process ID, and the command.
$ bash count-watches.sh | head -1
958 5014 node /path/to/app/node_modules/.bin/vite --port 5173
Excluding node_modules/, the test project had 910 files, including package.json and the test scripts. It had 48 folders, including the project folder itself, and the total (958) matched the sum. Since the limit is per user, watches used by other processes running under the same user are counted as well.
Fixing it by raising the limit
The troubleshooting page in the official Vite docs gives an example of raising the limit to 524288. Changing the value with sysctl takes effect immediately.
$ sudo sysctl fs.inotify.max_user_watches=524288
fs.inotify.max_user_watches = 524288
After I restarted the dev server in this state, no ENOSPC lines appeared at all. When I changed src/dir40/m1.js with the page open in the browser, Vite detected the change and reloaded the page, and the heading changed to v1 = 100.
$ npx vite --port 5173
VITE v8.3.8 ready in 166 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
11:06:54 PM [vite] (client) page reload src/dir40/m1.js
(以下省略)
A value changed with sysctl is reset when the machine reboots. To keep it after a reboot, place a config file in /etc/sysctl.d/. As described in the systemd sysctl.d man page, .conf files in this folder are read at boot. Name the file so that it ends in .conf.
$ echo 'fs.inotify.max_user_watches=524288' | sudo tee /etc/sysctl.d/99-inotify-watches.conf
fs.inotify.max_user_watches=524288
$ sudo sysctl --system
(省略)
* Applying /etc/sysctl.d/99-inotify-watches.conf ...
(省略)
fs.inotify.max_user_watches = 524288
sudo sysctl --system reloads all values from the system configuration folders. I ran it after lowering the limit to 100 and confirmed that the value changed to the one in the file (524288). The output also lists lines from other config files, so only the relevant line is kept here.
Fixing it by watching fewer files
If you do not have permission to change the limit, or if you have many files that you do not edit during development, such as test data or images, you can also exclude them from watching. In Vite, you can set the options for the file watching library (chokidar) in server.watch in the official docs. .git/ and node_modules/ are already excluded from watching.
I set the limit to 600, which is not enough to watch all the files and folders. When I started without any config, 365 ENOSPC lines were printed, and editing src/dir40/m1.js did not change the page. So I added the following vite.config.js to exclude fixtures/.
import { defineConfig } from 'vite';
export default defineConfig({
server: {
watch: {
// Exclude folders that are not edited during development from watching
ignored: ['**/fixtures/**'],
},
},
});
When I restarted with the same limit of 600, no ENOSPC lines appeared, and changing src/dir40/m1.js updated the heading to v1 = 100. Only exclude folders whose contents you do not edit during development.





