npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nano-spawn

v0.2.0

Published

Tiny process execution for humans — a better child_process

Downloads

89,456

Readme

Test coverage

Tiny process execution for humans — a better child_process

Features

No dependencies. Small package size: npm package minzipped size Install size

Despite the small size, this is packed with some essential features:

For additional features, please check out Execa.

Install

npm install nano-spawn

One of the maintainers @ehmicky is looking for a remote full-time position. Specialized in Node.js back-ends and CLIs, he led Netlify Build, Plugins and Configuration for 2.5 years. Feel free to contact him on his website or on LinkedIn!


Usage

Run commands

import spawn from 'nano-spawn';

const result = await spawn('echo', ['🦄']);

console.log(result.output);
//=> '🦄'

Iterate over output lines

for await (const line of spawn('ls', ['--oneline'])) {
	console.log(line);
}
//=> index.d.ts
//=> index.js
//=> …

Pipe commands

const result = await spawn('npm', ['run', 'build'])
	.pipe('sort')
	.pipe('head', ['-n', '2']);

API

spawn(file, arguments?, options?) default export

file: string
arguments: string[]
options: Options
Returns: Subprocess

Executes a command using file ...arguments.

This has the same syntax as child_process.spawn().

If file is 'node', the current Node.js version and flags are inherited.

Options

options.stdio, options.shell, options.timeout, options.signal, options.cwd, options.killSignal, options.serialization, options.detached, options.uid, options.gid, options.windowsVerbatimArguments, options.windowsHide, options.argv0

All child_process.spawn() options can be passed to spawn().

options.env

Type: object
Default: {}

Override specific environment variables. Other environment variables are inherited from the current process (process.env).

options.preferLocal

Type: boolean
Default: false

Allows executing binaries installed locally with npm (or yarn, etc.).

options.stdin, options.stdout, options.stderr

Type: string | number | Stream | {string: string}

Subprocess's standard input/output/error.

All values supported by node:child_process are available. The most common ones are:

Subprocess

Subprocess started by spawn().

await subprocess

Returns: Result
Throws: SubprocessError

A subprocess is a promise that is either resolved with a successful result object or rejected with a subprocessError.

subprocess.stdout

Returns: AsyncIterable<string>
Throws: SubprocessError

Iterates over each stdout line, as soon as it is available.

The iteration waits for the subprocess to end (even when using break or return). It throws if the subprocess fails. This means you do not need to call await subprocess.

subprocess.stderr

Returns: AsyncIterable<string>
Throws: SubprocessError

Same as subprocess.stdout but for stderr instead.

subprocess[Symbol.asyncIterator]()

Returns: AsyncIterable<string>
Throws: SubprocessError

Same as subprocess.stdout but for both stdout and stderr.

subprocess.pipe(file, arguments?, options?)

file: string
arguments: string[]
options: Options
Returns: Subprocess

Similar to the | symbol in shells. Pipe the subprocess'sstdout to a second subprocess's stdin.

This resolves with that second subprocess's result. If either subprocess is rejected, this is rejected with that subprocess's error instead.

This follows the same syntax as spawn(file, arguments?, options?). It can be done multiple times in a row.

await subprocess.nodeChildProcess

Type: ChildProcess

Underlying Node.js child process.

Among other things, this can be used to terminate the subprocess using .kill() or exchange IPC message using .send().

Result

When the subprocess succeeds, its promise is resolved with an object with the following properties.

result.stdout

Type: string

The output of the subprocess on standard output.

If the output ends with a newline, that newline is automatically stripped.

This is an empty string if either:

result.stderr

Type: string

Like result.stdout but for the standard error instead.

result.output

Type: string

Like result.stdout but for both the standard output and standard error, interleaved.

result.command

Type: string

The file and arguments that were run.

It is intended for logging or debugging. Since the escaping is fairly basic, it should not be executed directly.

result.durationMs

Type: number

Duration of the subprocess, in milliseconds.

result.pipedFrom

Type: Result | SubprocessError | undefined

If subprocess.pipe() was used, the result or error of the other subprocess that was piped into this subprocess.

SubprocessError

Type: Error

When the subprocess fails, its promise is rejected with this error.

Subprocesses fail either when their exit code is not 0 or when terminated by a signal. Other failure reasons include misspelling the command name or using the timeout option.

Subprocess errors have the same shape as successful results, with the following additional properties.

This error class is exported, so you can use if (error instanceof SubprocessError) { ... }.

subprocessError.exitCode

Type: number | undefined

The numeric exit code of the subprocess that was run.

This is undefined when the subprocess could not be started, or when it was terminated by a signal.

subprocessError.signalName

Type: string | undefined

The name of the signal (like SIGTERM) that terminated the subprocess, sent by either:

If a signal terminated the subprocess, this property is defined and included in the error message. Otherwise it is undefined.

Windows support

This package fixes several cross-platform issues with node:child_process. It brings full Windows support for:

  • Node modules binaries (without requiring the shell option). This includes running npm ... or yarn ....
  • .cmd, .bat, and other shell files.
  • The PATHEXT environment variable.
  • Windows-specific newlines.

Alternatives

nano-spawn's main goal is to be small, yet useful. Nonetheless, depending on your use case, there are other ways to run subprocesses in Node.js.

Execa

Execa is a similar package: it provides the same features, but more. It is also built on top of node:child_process, and is maintained by the same people.

On one hand, it has a bigger size: Install size

On the other hand, it provides a bunch of additional features: scripts, template string syntax, synchronous execution, file input/output, binary input/output, advanced piping, verbose mode, graceful or forceful termination, IPC, shebangs on Windows, and much more. Also, it is very widely used and battle-tested.

We recommend using Execa in most cases, unless your environment requires using small packages (for example, in a library or in a serverless function). It is definitely the best option inside scripts, servers, or apps.

node:child_process

nano-spawn is built on top of the node:child_process core module.

If you'd prefer avoiding adding any dependency, you may use node:child_process directly. However, you might miss the features nano-spawn provides: proper error handling, full Windows support, local binaries, piping, lines iteration, interleaved output, and more.

import {execFile} from 'node:child_process';
import {promisify} from 'node:util';

const pExecFile = promisify(execFile);

const result = await pExecFile('npm', ['run', 'build']);

Maintainers