Below you can see simplified view of an issue. Basically, I'm able to call task1.js using gulp.series in tasks task2,3.js, but once I add same code to call task1.js in task4.js - Task never defined: task1
error gets thrown.
There are more tasks in the tasks folder than in file structure example below.
I've got three tasks,
...
/tasks
build.js
clean.js
dev.js
gulpfile.babel.js
...
all of them required in gulpfile.babel.js using the require-dir package
import requireDir from 'require-dir';
requireDir('./tasks', {recurse: true});
This allows me to call a task from clean.js at dev.js, and it works fine.
import gulp from 'gulp';
gulp.task('dev', gulp.series('clean');
But after I add same code structure at build.js.
import gulp from 'gulp';
gulp.task('build', gulp.series('clean');
it somehow breaks gulp stream (I guess), so now on any task call I get:
$gulp dev
-AssertionError [ERR_ASSERTION]: Task never defined: clean.
$gulp -v
[11:50:11] CLI version 2.0.1
[11:50:11] Local version 4.0.0
The
series
andparallel
functions of gulp 4 do not create a task definition as its README seems to suggest, but instead they both run the tasks in parameter. In order to work as intended, one need to surround the call with a closure.So, to fix the excerpt
it is necessary to add the closure:
I had a similar setup where I had recursively
require
'd all tasks under a directory. And after updating to gulp 4 started getting error Task never defined.I tried Pedro solution, but this caused another error:
The solution was fairly simple for me, just import the missing tasks.
For those migrating from gulp v3 to v4 or are using
gulp.task()
to define tasks in gulp v4 and get this error message:Task never defined
, the problem usually lies here:From gulpjs documentation re: gulp.series and gulp.parallel documentation.
Here is what that means. There are two ways to create tasks:
When you use version 1 (
gulp.task…
) you cannot refer to that task by its string name until it has been registered. So you cannot do this:Results in
That is a forward reference, composing a task (using
gulp.series
orgulp.parallel
) and referring to a task by its string name (in the above case'sass2css'
) before it has been registered. (calling "gulp.task(…..)
" is the act of registering) Putting thegulp.task('sass2css',...)
first fixes the problem.If you use version two of defining a task:
you are now using a named function to register a task and do not need to use its name as a string. So this now works:
followed by (or preceded by - either works):
The original OP used this and it worked:
Noted that
clean.js
got imported beforedev.js
so that was okay.This didn't work:
because the string-referenced task,
'clean'
gets imported (and thus registered) afterbuild.js
where it is referenced - thus creating an illegal forward reference to a string-referenced task.So there are two standard ways to fix this error:
Use named functions to define tasks not
gulp.task('someTask',...)
. Then it doesn't matter the order of using those named functions when composing other tasks, i.e., when usinggulp.series
orgulp.parallel
. And there are other advantages to using named functions, such as passing arguments, so this is the best option.If you do use the older gulp v3
gulp.task
method of creating tasks with string references, be careful to not refer to those tasks until after the task is actually created.Also see my answer at task never defined error for fixing another problem which results in the same error message. Specifically using
gulp.task('someTask', ['anotherTask'], function(){})
synatx in a gulp4 file.