gulp.src().on('error', ()=>{}) is not getting triggered when file specified in src is not found

969 Views Asked by At

I am trying to concatinate multiple files using gulp and gulp-concat modules of npm.

The following is the code snippet I have used.

gulp.src(['../a.js', '../b.js', '../c.js'])
    .pipe(concat('destination.js'))
    .pipe(gulp.dest('destination/path/'))
    .on('error', () => {
        console.error('Error');
     })
    .on('finish', () => {
        console.log('Success');
    });

The error event listener is not getting triggered when the file for suppose b.js or anyother is not found in the specified path.

The event finish is getting triggered anyway.

How do find error such as file not found in this process?

Thanks for the help in advance

2

There are 2 best solutions below

0
Krishna E On BEST ANSWER

I finally found a solution for this from the below post

How to make Gulp.src fail if a file is missing?

I Used the files-exist npm package to check if all files exist or not.

0
Anushil Kumar On

You can use gulp-plumber to get error while build.

Example:

Step 1: Install gulp-plumber:

npm i gulp-plumber --save-dev

Step 2: Require the gulp plmber module in gulpfile:

var plumber = require('gulp-plumber');   

Step 3: Usage

gulp.src(['../a.js', '../b.js', '../c.js'])
.pipe(plumber())
.pipe(concat('destination.js'))
.pipe(plumber.stop())
.pipe(gulp.dest('dist/js'));