node.jsgulpchild-process

How handle the child process errors in Gulp?


The error in child process not always means that something fatal occurred. For example in "vue-tsc" case, if there will be some TypeScript errors, the error parameter will be not null while "vue-tsc" application itself will finish the execution will full checking of all actual TypeScript/Vue files:

import ChildProcess from "child_process";

export default function childProcessTask(): ChildProcess.ChildProcess {
  return ChildProcess.exec(
     `npx vue-tcs --noEmit`,
     { encoding: "utf-8" },
     (error: ChildProcess.ExecException | null, stdout: string, stderr: string): void => {
       console.log(error);
       console.log(stdout);
       console.error(stderr);
  }
}

Anyway, it was for example, please do not recommend me the "vue-tsc" options in this question. Instead of "vue-tsc", any other utility can be so the solution actual for "vue-tsc" will not work.

My question is, how to handle the errors in child processes for the Gulp case? If to use above task function in Gulp and something will occur in child process, whole Gulp chain will crush. How to prevent it?


Solution

  • A process shouldn't be returned from a task if you don't need Gulp to catch errors from it. Instead it can be:

    export default function childProcessTask(cb): void {
      ChildProcess.exec(
         ...
         (error: ChildProcess.ExecException | null, stdout: string, stderr: string): void => {
           ...
           cb()
         }
      }
    }