Spawn or fork a child process with a promise property resolved on exit with stdout, stderr and code.
Last updated a year ago by zvr .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install spawncommand 
SYNC missed versions from official npm registry.


npm version

SpawnCommand will run the spawn or fork methods from the child_process module, and add a promise property to the returned process instance. The promise will be resolved on process exit with an object consisting of code, stdout and stderr properties.

yarn add spawncommand

Table Of Contents


SpawnCommand can be used by importing the default spawn and named fork exports.

import spawn, { fork } from 'spawncommand'


The package's main type is ChildProcessWithPromise which enriches the standard ChildProcess with a promise property.

import('child_process').ChildProcess child_process.ChildProcess

_spawncommand.ChildProcessWithPromise: A child process with an extra promise property.

Name Type Description
promise* !Promise<!_spawncommand.PromiseResult> A promise resolved when the process exits.
spawnCommand* string The spawn arguments joined by whitespace.


Name Type Description
stdout* string The accumulated result of the stdout stream.
stderr* string The accumulated result of the stderr stream.
code* number The code with which the process exited.

  module: string,
  args: string[],
  options?: SpawnOptions,
): ChildProcessWithPromise

Spawns a command and returns a ChildProcess instance with the promise property resolved on exit. The promise will be rejected if an error was encountered when trying to spawn the process.

import('child_process').SpawnOptions child_process.SpawnOptions

import spawn from 'spawncommand'

(async () => {
  const { promise } = spawn('echo', ['hello world'])
  const { stderr, stdout, code } =  await promise
    stderr, stdout, code,
  }, null, 2))
  "stderr": "",
  "stdout": "hello world\n",
  "code": 0

The returned object is a ChildProcess and all of its properties can be accessed in the standard way.

import spawnCommand from 'spawncommand'

(async () => {
  const { stdout, promise } = spawnCommand('echo', ['hello world'])

  await promise
hello world

  module: string,
  args: string[],
  options?: ForkOptions,
): ChildProcessWithPromise

Forks a Node.js module and adds a promise property to the returned ChildProcess.

import('child_process').ForkOptions child_process.ForkOptions

import { fork } from 'spawncommand'

(async () => {
  const { promise } = fork('node_modules/.bin/alanode',
    ['example/spawn.js'], {
      stdio: 'pipe',
  const { stdout } =  await promise
  "stderr": "",
  "stdout": "hello world\n",
  "code": 0

The pipe option needs to be set in order to gather the output of the stderr and stdout streams (or an array for older versions of Node.js when this does not work).


Art Deco © Art Deco 2019 Tech Nation Visa Tech Nation Visa Sucks

Current Tags

  • 2.2.0                                ...           latest (a year ago)

8 Versions

  • 2.2.0                                ...           a year ago
  • 2.1.2                                ...           2 years ago
  • 2.1.1                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
Maintainers (1)
Today 0
This Week 1
This Month 3
Last Day 1
Last Week 1
Last Month 10
Dependencies (1)
Dev Dependencies (4)

Copyright 2014 - 2017 © |