We are switching from gulp@3.9.1
to gulp@4
and are having trouble switching over. When we run gulp watch
, we are getting the following errors and trying to figure out how to resolve it.
What is the proper way to convert the gulp watch
task to work with gulp@4
?
Error message
AssertionError [ERR_ASSERTION]: Task never defined:
minify-css
Command: gulp watch
- This should run
minify-js
thenminify-css
in order minify-js
should run afterclean-scripts
has completed successfullyminify-css
should run afterclean-css
has completed successfully
Current tasks.
var gulp = require('gulp'),
cssmin = require('gulp-clean-css'),
clean = require('gulp-clean'),
uglify = require('gulp-uglify');
var src = {
js: 'js/some-dir/**/*.js',
css: 'css/some-dir/**/*.css'
};
var dest = {
js: 'js/dest/some-dir/**/*.js',
css: 'css/dest/some-dir/**/*.css'
};
gulp.task('clean-css', function() {
return gulp.src(dest.css)
.pipe(clean({read:false, force: true});
});
gulp.task('minify-css', ['clean-css'], function() {
gulp.src(src.css)
.pipe(cssmin())
.pipe(gulp.dest(dest.css));
});
gulp.task('clean-scripts', function() {
return gulp.src(dest.js)
.pipe(clean({read:false, force: true});
});
gulp.task('minify-js', ['clean-scripts'], function() {
gulp.src(src.js)
.pipe(uglify())
.pipe(gulp.dest(dest.js));
});
gulp.task('watch', ['minify-js', 'minify-css'], function() {
gulp.watch(src.js, ['minify-js']);
gulp.watch(src.css, ['minify-css']);
});
We tried doing this, but it resulted in the error message
gulp.task('watch', gulp.series('minify-js', 'minify-css', function() {
gulp.watch(src.js, ['minify-js']);
gulp.watch(src.css, ['minify-css']);
}));
As @Abdaylan suggested, I also advocate switching to functions. Nevertheless, so you can see where your code was wrong, I have fixed it here. Gulp 4 does not use this syntax:
Gulp 4:
or
gulp.parallel
. So you can use yourgulp.task
syntax (rather than named functions) if you modify them to use the signatures that gulp 4 supports as I did in your modified code at the top of this answer.Gulp 4 with named functions:
So with named functions, the tasks are not referred to as strings.
See also task never defined for other potential problems when migrating from gulp3 to gulp4 with the same error message.
I would recommend converting your
minify-js
,minify-css
,clean-scripts
andclean-css
tasks to functions:I just ran into this a couple days ago myself. What worked for me was to run each task in its own
gulp.watch()
with thegulp.series()
on the watch task call instead of the watch task itself. For example: