ENOENT error in gulp.src when glob pattern does not match any files
0 reputation · 03 Jun 2023, 17:07 UTC
0 reputation · 03 Jun 2023, 17:07 UTC
When developing a gulp build, the goal is to reliably detect missing source files through the ENOENT error emitted by gulp.src, while also ensuring that file‑watching tasks do not accumulate listeners that cause a memory leak.
The current gulp 4 documentation describes the ENOENT behavior for non‑existent glob patterns but does not specify whether gulp.watch should automatically close its internal chokidar watchers after a task finishes or expose an option like ignoreInitial to reduce listener churn. This leaves developers to decide whether to reuse a single watcher or to access internal APIs for cleanup, which may break in future releases.
What is the recommended approach for handling the ENOENT error in gulp.src when a glob pattern yields no matches? Should gulp.watch provide automatic watcher cleanup or an ignore‑initial flag to prevent listener accumulation? Are there any version‑specific behaviors in chokidar that affect the need for manual cleanup?
32275 reputation · 03 Jun 2023, 20:52 UTC
When gulp.src receives a glob that matches no files, it emits an ENOENT error on the vinyl stream. Attach an error handler to decide whether the build should fail or treat the empty match as acceptable. Do not use ENOENT as control flow; if missing files are expected, check with fs or loosen the glob instead.
gulp.src('src/**/*.js')
.on('error', err => {
if (err.code === 'ENOENT') {
console.warn('No source files matched – verify paths or allow empty match');
// emit() to continue or throw to fail
}
})
.pipe(gulp.dest('dist'));If empty matches are valid, prefer a pre‑check with Node's fs.existsSync or a glob that permits zero results, rather than relying on the error.
gulp.watch returns the underlying chokidar watcher; gulp does not close it when the task function ends. Store the returned watcher and call watcher.close() when you want to stop watching (e.g., on process shutdown). Reuse a single watcher per glob and register change listeners once to avoid MaxListenersExceededWarning. Use the ignoreInitial option to suppress events for files that exist at startup.
const watcher = gulp.watch('src/**/*.js', { ignoreInitial: true }, gulp.series('build'));
// later, e.g. on SIGINT
process.on('SIGINT', async () => {
await watcher.close();
process.exit(0);
});Listener growth usually comes from creating a new watcher inside a repeatedly invoked task or adding listeners per change event, not from a chokidar defect.
gulp.src('nonexistent/**/*.js') and an error handler; confirm the ENOENT error is caught and the build behaves as you chose.node --trace-warnings ./node_modules/.bin/gulp watch and watch the listener count; it should stay steady when you reuse one watcher.watcher.close() on the stored watcher and verify the process exits cleanly and file descriptors are released.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.