添加images.js后出现gulp-notify文件未找到错误,如何解决?
Gulp-notify 报错:File not found with singular glob 问题排查与解决
执行gulp命令时出现以下报错,尽管图片转换流程最终完成,但错误提示持续存在:
[21:33:06] gulp-notify: [IMAGES] Error: File not found with singular glob: C:/Users/Barcelona/Desktop/Filancer_gulp/dist/img/ (if this was purposeful, use
allowEmptyoption)
完整终端输出
Barcelona@DESKTOP-DJ763J5 MINGW64 ~/Desktop/Filancer_gulp $ gulp. [21:33:06] gulp-notify: [IMAGES] Error: File not found with singular glob: C:/Users/Barcelona/Desktop/Filancer_gulp/dist/img/ (if this was purposeful, use `allowEmpty` option) [gulp-version-number] Output to file: gulp/version.json [21:33:06] Finished 'html' after 405 ms [21:33:06] Finished 'copy' after 407 ms [21:33:06] Finished 'scss' after 407 ms [21:33:06] asset app.min.js 140 KiB [emitted] (name: main) webpack 5.88.2 compiled successfully [21:33:06] Finished 'js' after 606 ms [21:33:12] gulp-imagemin: Minified 0 images [21:33:12] Finished 'images' after 6.32 s [21:33:12] Starting 'watcher'... [21:33:12] Starting 'server'... [Browsersync] Access URLs: ----------------------------------- Local: http://localhost:3000 External: http://10.0.0.106:3000 ----------------------------------- UI: http://localhost:3001 UI External: http://localhost:3001 ----------------------------------- [Browsersync] Serving files from: ./dist/
当前使用的images.js代码
import webp from "gulp-webp"; import imagemin from "gulp-imagemin"; export const images = () => { return app.gulp.src(app.path.src.images) .pipe(app.plugins.plumber( app.plugins.notify.onError({ title: "IMAGES", message: "Error: <%= error.message %>" })) ) .pipe(app.plugins.newer(app.path.build.images)) .pipe(webp()) .pipe(app.gulp.dest(app.path.build.images)) .pipe(app.gulp.src(app.path.build.images)) .pipe(app.plugins.newer(app.path.build.images)) .pipe(imagemin({ progressive: true, svgoPlugins: [{ removeViewBox: false }], interlaced: true, optimizationLevel: 3 // 0 to 7 })) .pipe(app.gulp.dest(app.path.build.images)) .pipe(app.gulp.src(app.path.src.svg)) .pipe(app.gulp.dest(app.path.build.images)) .pipe(app.plugins.browsersync.stream()); }
错误原因
报错的核心是直接将目录路径传给gulp.src(),而非匹配文件的glob规则。代码中这一行:
.pipe(app.gulp.src(app.path.build.images))
app.path.build.images对应的值是目标目录路径,gulp.src()接收纯目录路径时,会尝试读取目录下的文件,但首次执行时目录为空(还未生成webp文件),就会触发“singular glob”错误。终端输出里的gulp-imagemin: Minified 0 images也验证了这一步没有找到可处理的文件。
解决方法
方法1:添加文件匹配glob规则(推荐)
修改读取build目录的代码行,明确指定要处理的文件类型,避免直接传入目录路径:
// 原代码 .pipe(app.gulp.src(app.path.build.images)) // 修改为(匹配目录下所有文件) .pipe(app.gulp.src(`${app.path.build.images}**/*`))
或者更精确地匹配图片格式:
.pipe(app.gulp.src(`${app.path.build.images}**/*.{png,jpg,jpeg,webp}`))
方法2:允许空目录场景
如果需要保留直接传入目录的写法,可给gulp.src()添加allowEmpty: true选项,允许目录为空的情况:
.pipe(app.gulp.src(app.path.build.images, { allowEmpty: true }))
额外检查
确认path.js中app.path.build.images的配置末尾带有斜杠(/),避免拼接glob规则时出现路径错误。
内容的提问来源于stack exchange,提问作者Дмитрий А
相关产品推荐
相关产品推荐

