问题
My gulp file is not emitting any errors on either standard gulp methods like gulp.src() with non existant paths nor are stream errors being called by their handler. I've included a simple file that silently fails, printing only the starting.. and finished.. default messages.
var //
gulp = require( "gulp" ),
gulpUtil = require( "gulp-util" ),
sass = require( "gulp-ruby-sass");
gulp.task( "default", function() {
gulp.src( "path-that-does-not-exist.scss" )
.pipe( sass() )
.on( "error", function( err ) {
console.log( "this should print" );
})
.pipe( gulp.dest( "./client-side/public/compiled" ) );
});
回答1:
Okay, so Gulp is working "correctly." But what I'm hearing in your question is that it would be nice if Gulp could just tell you if a file doesn't exist.
I use gulp-expect-file as such
var coffee = require('gulp-coffee');
var expect = require('gulp-expect-file');
gulp.task('mytask', function() {
var files = ['idontexist.html'];
return gulp.src(files)
.pipe(expect(files))
.pipe(coffee());
});
Now you'll see this in the terminal:

回答2:
If the path does not exist, gulp.src successfully pushes exactly nothing through the pipe, the sass and dest tasks successfully are never called, and with that, gulp.src's stream ends, signaling the default task is complete, and gulp exits. It has done exactly what you've told it to. :D
回答3:
I agree with @adamjgrant use gulp-expect-file.
var expect = require('gulp-expect-file'),
gulp = require('gulp');
gulp.task('task', function() {
var files = ['src/js/*.js'];
gulp
.src(files)
.pipe(expect(files));
});
PS: Grunt.js handles this with nonull:true
lint: {
src: ['gruntfile.js', 'gulpfile.js'],
nonull: true
}
来源:https://stackoverflow.com/questions/22343591/gulp-silently-failing-no-errors-printed-to-console