velite icon indicating copy to clipboard operation
velite copied to clipboard

Error: EMFILE: too many open files

Open trebeljahr opened this issue 1 year ago • 1 comments

I am encountering an error with velite's watchmode during npm run dev.

The full stack trace

Error: EMFILE: too many open files, watch '/Users/rico/projects/trebeljahr.com/src/content/Notes/assets/midjourney-gallery/Trebeljahr_an_impressionistic_painting_of_a_beautiful_flower_ga_ae6f3c9a-4729-42b4-bbc3-bd582fd87dbe.webp'
 at FSWatcher.<computed> (node:internal/fs/watchers:247:19)
    at watch (node:fs:2473:36)
    at createFsWatchInstance (file:///Users/rico/projects/trebeljahr.com/node_modules/velite/dist/velite-CMIMbt4v.js:354:16)
    at setFsWatchListener (file:///Users/rico/projects/trebeljahr.com/node_modules/velite/dist/velite-CMIMbt4v.js:399:19)
    at NodeFsHandler._watchWithNodeFs (file:///Users/rico/projects/trebeljahr.com/node_modules/velite/dist/velite-CMIMbt4v.js:553:22)
    at NodeFsHandler._handleFile (file:///Users/rico/projects/trebeljahr.com/node_modules/velite/dist/velite-CMIMbt4v.js:619:29)
    at NodeFsHandler._addToNodeFs (file:///Users/rico/projects/trebeljahr.com/node_modules/velite/dist/velite-CMIMbt4v.js:843:31)
Emitted 'error' event on FSWatcher instance at:
    at FSWatcher._handleError (file:///Users/rico/projects/trebeljahr.com/node_modules/velite/dist/velite-CMIMbt4v.js:1384:18)
    at NodeFsHandler._addToNodeFs (file:///Users/rico/projects/trebeljahr.com/node_modules/velite/dist/velite-CMIMbt4v.js:851:26) {
  errno: -24,
  syscall: 'watch',
  code: 'EMFILE',
  path: '/Users/rico/projects/trebeljahr.com/src/content/Notes/assets/midjourney-gallery/Trebeljahr_an_impressionistic_painting_of_a_beautiful_flower_ga_ae6f3c9a-4729-42b4-bbc3-bd582fd87dbe.webp',
  filename: '/Users/rico/projects/trebeljahr.com/src/content/Notes/assets/midjourney-gallery/Trebeljahr_an_impressionistic_painting_of_a_beautiful_flower_ga_ae6f3c9a-4729-42b4-bbc3-bd582fd87dbe.webp'
}

The problem seems to be that I have too many files in the content directory that velite tries to watch.

For context, I am storing around 7000 assets (mostly .jpg images) + another 500 or so content (.md) files in my Obsidian repo, that then get ingested by velite during the build/runtime process of next.js

The error disappears as soon as I temporarily move the assets out of the content directory that velite is trying to watch, in other words there seems to be a limit to the files velite can watch which is a bit annoying...

my next.js config part for running velite looks like this and is pretty much copy pasted from the docs

const isDev = process.argv.indexOf("dev") !== -1;
const isBuild = process.argv.indexOf("build") !== -1;
if (!process.env.VELITE_STARTED && (isDev || isBuild)) {
  process.env.VELITE_STARTED = "1";
  const { build } = await import("velite");
  await build({ watch: isDev, clean: !isDev, logLevel: "error" });
}

I've been trying to look into the docs for options to give to the build command, but didn't find any option to increase the "max" file watcher limit or to tell velite to ignore directories/files.

But something like that would solve the problem since I really don't need to watch the assets part of the content folder for rebuilding all the time with velite.

trebeljahr avatar Dec 18 '24 17:12 trebeljahr

Unfortunately, this is a bug in the upstream package. I will try to switch to @parcel/watcher in the near future

zce avatar Dec 20 '24 02:12 zce