Search code examples
gulp

Gulp task never defined: default. Tree shows no default task


I am trying to modernize an old gulpfile for launching processes, but running into issues. When I run gulp:

PS C:\Users\me\Desktop\_REPOS\gknode> gulp
[18:42:50] Using gulpfile ~\Desktop\_REPOS\gknode\gulpfile.js
[18:42:50] Task never defined: default

gulpfile.js

gulp=require('gulp')

//Because gulp.task(name, deps, func) was replaced by gulp.task(name, gulp.{series|parallel}(deps, func))

gulp.task('node-server-start', gulp.series(
  function (cb) {spawn('node', ['nodeapi/nodeapi.js'], {stdio: 'inherit'}) }
  )
);
gulp.task('ng-serve', gulp.series(
  function (cb) {spawn('ng', ['serve'], {stdio: 'inherit'}) }
  )
);
gulp.task('start', gulp.parallel(['ng-serve', 'node-server-start'], function () {console.log('both servers launched on localhost:4200')}));

Task tree:

PS C:\Users\me\Desktop\_REPOS\gknode> gulp --tasks
[18:43:00] Tasks for ~\Desktop\_REPOS\gknode\gulpfile.js
[18:43:00] ├─┬ node-server-start
[18:43:00] │ └─┬ <series>
[18:43:00] │   └── <anonymous>
[18:43:00] ├─┬ ng-serve
[18:43:00] │ └─┬ <series>
[18:43:00] │   └── <anonymous>
[18:43:00] └─┬ start
[18:43:00]   └─┬ <parallel>
[18:43:00]     ├─┬ ng-serve
[18:43:00]     │ └─┬ <series>
[18:43:00]     │   └── <anonymous>
[18:43:00]     ├─┬ node-server-start
[18:43:00]     │ └─┬ <series>
[18:43:00]     │   └── <anonymous>
[18:43:00]     └── <anonymous>

Is my problem the anonymous task at the end of the start task?


Solution

  • In order to be able to run just gulp, you need to specify a task called default. Some other points:

    • You don't need to wrap a single task function in gulp.series.
    • gulp.parallel is expecting a list of task functions (names or anonymous functions), not an array.
    • You may run into issues when those callbacks specified for the server spawning task function are never called.

    The last point may be unnecessary to get the code to work but I've included it in the following which should get the job done:

    const gulp = require('gulp');
    const { spawn } = require('child_process');
    
    gulp.task('node-server-start', function(cb) {
        const server = spawn('node', ['nodeapi/nodeapi.js'], { stdio: 'inherit' });
        server.on('close', () => cb());
    });
    
    gulp.task('ng-serve', function(cb) {
        const server = spawn(
            /^win/.test(process.platform) ? 'ng.cmd' : 'ng',
            ['serve'],
            { stdio: 'inherit' }
        );
        server.on('close', () => cb());
    });
    
    gulp.task(
        'start',
        gulp.parallel('ng-serve', 'node-server-start', function(cb) {
            console.log('both servers launched on localhost:4200');
            cb();
        })
    );
    
    gulp.task('default', gulp.series('start'));
    
    

    Update:

    Updated with platform-agnostic solution.