Skip to content
spawn and control child processes in node.js with ease
Find file
New pull request
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


nexpect is a node.js module for spawning child applications (such as ssh) and seamlessly controlling them using javascript callbacks. nexpect is based on the ideas of the expect library by Don Libes and the pexpect library by Noah Spurrier.


node.js has good built in control for spawning child processes. nexpect builds on these core methods and allows developers to easily pipe data to child processes and assert the expected response. nexpect also chains, so you can compose complex terminal interactions.


Installing npm (node package manager)

  $ curl | sh

Installing nexpect

  $ npm install nexpect


Basic usage

The core method, nexpect.spawn(command, [params], [options]), takes three parameters:

  • command: The command that you wish to spawn
  • params: The argv that you want to pass to the child process
  • options: An object literal which may contain
    • verbose: Writes the stdout for the child process to process.stdout of the current process.
    • stripColors: Strips any ANSI colors from the output for .expect() and .wait() statements.
    • ignoreCase: Ignores the case of any output from the child process.

Lets take a look at some sample usage:

  var nexpect = require('nexpect');

  nexpect.spawn("echo", ["hello"])
         .run(function (err) {
           if (!err) {
             console.log("hello was echoed");

  nexpect.spawn("ls -la /tmp/undefined")
         .expect("No such file or directory")
         .run(function (err) {
           if (!err) {
             console.log("checked that file doesn't exists");

         .run(function (err) {
           if (!err) {
             console.log("node process started, console logged, process exited");
           else {

If you are looking for more examples take a look at the examples, and tests.


All tests are written with vows:

  $ npm test


Elijah Insua Marak Squires, and Charlie Robbins.

Something went wrong with that request. Please try again.