package
Tiny process execution for humans — a better child_process
npm install nano-spawnGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
60.6 kB
Includes dependencies.
Tiny process execution for humans — a better
child_process
No dependencies. Small package size:
Despite the small size, this is packed with some essential features:
npx.For additional features, please check out Execa.
npm install nano-spawnimport spawn from 'nano-spawn';
const result = await spawn('echo', ['🦄']);
console.log(result.output);
//=> '🦄'for await (const line of spawn('ls')) {
console.log(line);
}
//=> node_modules
//=> source
//=> test
//=> …const result = await spawn('npm', ['run', 'build'])
.pipe('sort')
.pipe('head', ['-n', '2']);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.
All child_process.spawn() options can be passed to spawn().
Cancellation with options.signal waits for the subprocess to exit and its standard streams to close before rejecting. Stream errors still reject immediately. If the subprocess ignores the termination signal, the promise stays pending.
Type: object
Default: {}
Override specific environment variables. Other environment variables are inherited from the current process (process.env).
Type: boolean
Default: false
Allows executing binaries installed locally with npm (or yarn, etc.).
Type: string
Input to write to the subprocess's standard input.
This cannot be set together with the stdin or stdio options, or on the destination of subprocess.pipe(). For incremental or binary input, write to the stdin of subprocess.nodeChildProcess instead.
const result = await spawn('gh', ['auth', 'login', '--with-token'], {input: token});Type: string | number | Stream
Subprocess's standard input/output/error.
All values supported by node:child_process are available. The most common ones are:
'pipe' (default value): returns the output using result.stdout, result.stderr and result.output.'inherit': uses the current process's input/output. This is useful when running in a terminal.'ignore': discards the input/output.Stream: redirects the input/output from/to a stream. For example, / can be used, once the stream's event has been emitted.To pass input as a string, use the input option instead.
Subprocess started by spawn().
Returns: Result
Throws: SubprocessError
A subprocess is a promise that is either resolved with a successful result object or rejected with a subprocessError.
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. The only exception is an error in one of its stdio streams: then it throws right away, since the subprocess might keep running. It can be terminated through nodeChildProcess if needed. For a piped subprocess, see the completion rules of subprocess.pipe().
Returns: AsyncIterable<string>
Throws: SubprocessError
Same as subprocess.stdout but for stderr instead.
Returns: AsyncIterable<string>
Throws: SubprocessError
Same as subprocess.stdout but for both stdout and stderr.
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. The only exception is a source terminated by SIGPIPE, which the destination stopping to read normally causes. A source which ends with SIGPIPE for an unrelated reason is treated the same way, and its error stays reachable through pipedFrom. The pipeline takes the destination's outcome, like a shell does. When a source is piped to several destinations, one of them stopping to read also ends the input of the others.
After piping is set up, a piped subprocess waits for both subprocess promises to settle, including after a stdio error. The source promise returned by spawn() still reports its own stdio errors immediately.
This follows the same syntax as spawn(file, arguments?, options?). It can be done multiple times in a row.
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().
When the subprocess succeeds, its promise is resolved with an object with the following properties.
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:
stdout option is set to another value than 'pipe' (its default value).subprocess.stdout or subprocess[Symbol.asyncIterator].Type: string
Like result.stdout but for the standard error instead.
Type: string
Like result.stdout but for both the standard output and standard error, interleaved.
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.
Type: number
Duration of the subprocess, in milliseconds.
Type: Result | SubprocessError | undefined
If subprocess.pipe() was used, the result or error of the other subprocess that was piped into this subprocess.
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) { ... }.
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.
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. Otherwise it is undefined. The signal is included in the error message, unless the error has a cause, for example when the subprocess was canceled while it was running.
Type: boolean
Whether the signal option was aborted when this failure was reported. This does not necessarily mean that cancellation caused the failure.
This package fixes several cross-platform issues with node:child_process. It brings full Windows support for:
shell option). This includes running npm ... or yarn .....cmd, .bat, and other shell files.PATHEXT environment variable.When the shell option is not set, .cmd, .bat, and other files except .exe and .com are run through cmd.exe, so their arguments are escaped. This uses the same escaping as Rust, which works whether the batch file reads its arguments (%1, "%~1") or forwards them (%*), like Node modules binaries do.
[!WARNING] Batch files can still misuse their arguments, which no escaping can prevent:
%~1receives the escaped argument: each"is doubled, and so are the backslashes before a"or at the end.- Using
%~1outside double quotes, or%1inside double quotes (likeif "%1" == ""), letscmd.exeinterpret the argument's special characters, which allows command injection.- Forwarding arguments with
call, or enabling delayed expansion, makescmd.exeexpand%or!in them again.Also, arguments cannot contain line breaks, and the escaping requires the
cmd.exedefaults: command extensions enabled and delayed expansion disabled.
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 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:
On the other hand, it provides a bunch of additional features: scripts, template string syntax, synchronous execution, file input/output, binary input/output, , , or , , , . Also, it is and .
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_processnano-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']);ehmicky
ehmicky@gmail.com
stdin/stdout/stderr.