Function spawn

  • Execute a file.

    Think of this as a mix of child_process.execFile and child_process.spawn.

    Parameters

    • file: string

      The program/script to execute.

    • Optionalarguments: readonly string[]

      Arguments to pass to file on execution.

    • Optionaloptions: Options<string>

    Returns ExecaChildProcess<string>

    A child_process instance, which is enhanced to also be a Promise for a result Object with stdout and stderr properties.

    import execa = require('execa');

    (async () => {
    const {stdout} = await execa('echo', ['unicorns']);
    console.log(stdout);
    //=> 'unicorns'

    // Cancelling a spawned process
    const subprocess = execa('node');
    setTimeout(() => { spawned.cancel() }, 1000);
    try {
    await subprocess;
    } catch (error) {
    console.log(subprocess.killed); // true
    console.log(error.isCanceled); // true
    }
    })();

    // Pipe the child process stdout to the current stdout
    execa('echo', ['unicorns']).stdout.pipe(process.stdout);
  • Parameters

    • file: string
    • Optionalarguments: readonly string[]
    • Optionaloptions: Options<null>

    Returns ExecaChildProcess<Buffer>

  • Parameters

    • file: string
    • Optionaloptions: Options<string>

    Returns ExecaChildProcess<string>

  • Parameters

    • file: string
    • Optionaloptions: Options<null>

    Returns ExecaChildProcess<Buffer>

Methods

  • Same as execa() except both file and arguments are specified in a single command string. For example, execa('echo', ['unicorns']) is the same as execa.command('echo unicorns').

    If the file or an argument contains spaces, they must be escaped with backslashes. This matters especially if command is not a constant but a variable, for example with __dirname or process.cwd(). Except for spaces, no escaping/quoting is needed.

    The shell option must be used if the command uses shell-specific features, as opposed to being a simple file followed by its arguments.

    Parameters

    • command: string

      The program/script to execute and its arguments.

    • Optionaloptions: Options<string>

    Returns ExecaChildProcess<string>

    A child_process instance, which is enhanced to also be a Promise for a result Object with stdout and stderr properties.

    import execa = require('execa');

    (async () => {
    const {stdout} = await execa.command('echo unicorns');
    console.log(stdout);
    //=> 'unicorns'
    })();
  • Parameters

    • command: string
    • Optionaloptions: Options<null>

    Returns ExecaChildProcess<Buffer>

  • Same as execa.command() but synchronous.

    Parameters

    • command: string

      The program/script to execute and its arguments.

    • Optionaloptions: SyncOptions<string>

    Returns ExecaSyncReturnValue<string>

    A result Object with stdout and stderr properties.

  • Parameters

    Returns ExecaSyncReturnValue<Buffer>

  • Execute a Node.js script as a child process.

    Same as execa('node', [scriptPath, ...arguments], options) except (like child_process#fork()): - the current Node version and options are used. This can be overridden using the nodePath and nodeArguments options. - the shell option cannot be used - an extra channel ipc is passed to stdio

    Parameters

    • scriptPath: string

      Node.js script to execute.

    • Optionalarguments: readonly string[]

      Arguments to pass to scriptPath on execution.

    • Optionaloptions: NodeOptions<string>

    Returns ExecaChildProcess<string>

    A child_process instance, which is enhanced to also be a Promise for a result Object with stdout and stderr properties.

  • Parameters

    • scriptPath: string
    • Optionalarguments: readonly string[]
    • Optionaloptions: Options<null>

    Returns ExecaChildProcess<Buffer>

  • Parameters

    • scriptPath: string
    • Optionaloptions: Options<string>

    Returns ExecaChildProcess<string>

  • Parameters

    • scriptPath: string
    • Optionaloptions: Options<null>

    Returns ExecaChildProcess<Buffer>

  • Execute a file synchronously.

    This method throws an Error if the command fails.

    Parameters

    • file: string

      The program/script to execute.

    • Optionalarguments: readonly string[]

      Arguments to pass to file on execution.

    • Optionaloptions: SyncOptions<string>

    Returns ExecaSyncReturnValue<string>

    A result Object with stdout and stderr properties.

  • Parameters

    • file: string
    • Optionalarguments: readonly string[]
    • Optionaloptions: SyncOptions<null>

    Returns ExecaSyncReturnValue<Buffer>

  • Parameters

    Returns ExecaSyncReturnValue<string>

  • Parameters

    Returns ExecaSyncReturnValue<Buffer>