1

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?

Rilcon42
  • 9,584
  • 18
  • 83
  • 167

1 Answers1

2

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.

Zwei
  • 1,279
  • 8
  • 16
  • I get: `both servers launched on localhost:4200 [23:08:16] Finished '' after 6.23 ms [23:08:16] 'ng-serve' errored after 8.45 ms [23:08:16] Error: spawn ng ENOENT at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19) at onErrorNT (internal/child_process.js:415:16) at process._tickCallback (internal/process/next_tick.js:63:19) [23:08:16] 'start' errored after 9.85 ms [23:08:16] 'default' errored after 15 ms [23:08:16] The following tasks did not complete: node-server-start [23:08:16] Did you forget to signal async completion?` – Rilcon42 Apr 05 '19 at 03:09
  • do I need to add something about async completion, or should I ask a new question to address that? – Rilcon42 Apr 05 '19 at 03:10
  • Could be related to [this issue](https://github.com/nodejs/node-v0.x-archive/issues/5841) -- updated the answer with a more platform-agnostic solution. With the `server.on('close', () => cb());` lines, that should help with async completion. The error you got is `spawn` saying it can't find the `ng` command to spawn. The async completion line at the end of it should resolve once that's sorted. – Zwei Apr 05 '19 at 04:02
  • Couldn't the OP just call `gulp start' as well? Or is there some convention in gulp that requires the `default` task to be defined? – Metro Smurf Sep 17 '21 at 17:50
  • @MetroSmurf There's no requirement for a `default` task unless you want to be able to just run `gulp` with no arguments as the OP did. `gulp start` would indeed accomplish the same thing. If you're using the 'modern' gulp exports syntax, it's the equivalent of assigning a task function to `exports.default` rather than to `exports.start`. – Zwei Sep 18 '21 at 18:13