{"name":"nano-spawn","version":"2.0.0","keywords":["spawn","exec","child","process","subprocess","execute","fork","execfile","file","shell","bin","binary","binaries","npm","path","local","zx","execa"],"author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"license":"MIT","_id":"nano-spawn@2.0.0","maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"},{"name":"ehmicky","email":"ehmicky@gmail.com"}],"homepage":"https://github.com/sindresorhus/nano-spawn#readme","bugs":{"url":"https://github.com/sindresorhus/nano-spawn/issues"},"ava":{"require":["./test/helpers/setup.js"],"timeout":"240s","concurrency":1},"dist":{"shasum":"f1250434c09ae18870d4f729fc54b406cf85a3e1","tarball":"http://registry-npm.ywwl.com/nano-spawn/download/nano-spawn-2.0.0.tgz","fileCount":12,"integrity":"sha512-tacvGzUY5o2D8CBh2rrwxyNojUsZNU2zjNTzKQrkgGJQTbGAfArVWXSKMBokBeeg6C7OLRGUEyoFlYbfeWQIqw==","signatures":[{"sig":"MEYCIQDIa3PhmirHw3cQyOjiTFpggJblb0aDw/LhNMJYsfyy/wIhAM1sTamrkY/dKlMjW0QuFwRTby8JXa8xnFR8Hmdzs6wm","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":44095,"size":12220,"noattachment":false,"key":"/nano-spawn/-/nano-spawn-2.0.0.tgz"},"type":"module","engines":{"node":">=20.17"},"exports":{"types":"./source/index.d.ts","default":"./source/index.js"},"funding":"https://github.com/sindresorhus/nano-spawn?sponsor=1","gitHead":"38da67877bec96474757282468f1754958aa3bab","scripts":{"test":"xo && c8 ava && npm run type","type":"tsd -t ./source/index.d.ts -f ./source/index.test-d.ts"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"repository":{"url":"git+https://github.com/sindresorhus/nano-spawn.git","type":"git"},"_npmVersion":"11.6.1","description":"Tiny process execution for humans — a better child_process","directories":{},"sideEffects":false,"_nodeVersion":"20.19.5","_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","xo":"^0.60.0","ava":"^6.1.3","tsd":"^0.32.0","tempy":"^3.1.0","get-node":"^15.0.1","path-key":"^4.0.0","typescript":"^5.8.3","@types/node":"^22.5.4","yoctocolors":"^2.1.1","log-process-errors":"^12.0.1"},"_npmOperationalInternal":{"tmp":"tmp/nano-spawn_2.0.0_1760141694076_0.12511041713081972","host":"s3://npm-registry-packages-npm-production"},"readme":"<h1 align=\"center\" title=\"nano-spawn\">\n\t<img src=\"media/logo.jpg\" alt=\"nano-spawn logo\">\n</h1>\n\n![Test coverage](https://img.shields.io/badge/coverage-100%25-green)\n<!-- [![Downloads](https://img.shields.io/npm/dm/nano-spawn.svg)](https://npmjs.com/nano-spawn) -->\n<!-- ![Dependents](https://img.shields.io/librariesio/dependents/npm/nano-spawn) -->\n\n> Tiny process execution for humans — a better [`child_process`](https://nodejs.org/api/child_process.html)\n\n## Features\n\nNo dependencies. Small package size: ![npm package minzipped size](https://img.shields.io/bundlejs/size/nano-spawn) [![Install size](https://packagephobia.com/badge?p=nano-spawn)](https://packagephobia.com/result?p=nano-spawn)\n\nDespite the small size, this is packed with some essential features:\n- [Promise-based](#spawnfile-arguments-options-default-export) interface.\n- [Iterate](#subprocesssymbolasynciterator) over the output lines.\n- [Pipe](#subprocesspipefile-arguments-options) multiple subprocesses and retrieve [intermediate results](#resultpipedfrom).\n- Execute [locally installed binaries](#optionspreferlocal) without `npx`.\n- Improved [Windows support](#windows-support).\n- Proper handling of [subprocess failures](#subprocesserror) and better error messages.\n- Get [interleaved output](#resultoutput) from stdout and stderr similar to what is printed on the terminal.\n- Strip [unnecessary newlines](#resultstdout).\n- Pass strings as [`stdin` input](#optionsstdin-optionsstdout-optionsstderr) to the subprocess.\n- Preserve the current [Node.js version and flags](#spawnfile-arguments-options-default-export).\n- Simpler syntax to set [environment variables](#optionsenv) or [`stdin`/`stdout`/`stderr`](#optionsstdin-optionsstdout-optionsstderr).\n- Compute the command [duration](#resultdurationms).\n\nFor additional features, please check out [Execa](#execa).\n\n## Install\n\n```sh\nnpm install nano-spawn\n```\n\n## Usage\n\n### Run commands\n\n```js\nimport spawn from 'nano-spawn';\n\nconst result = await spawn('echo', ['🦄']);\n\nconsole.log(result.output);\n//=> '🦄'\n```\n\n### Iterate over output lines\n\n```js\nfor await (const line of spawn('ls', ['--oneline'])) {\n\tconsole.log(line);\n}\n//=> index.d.ts\n//=> index.js\n//=> …\n```\n\n### Pipe commands\n\n```js\nconst result = await spawn('npm', ['run', 'build'])\n\t.pipe('sort')\n\t.pipe('head', ['-n', '2']);\n```\n\n## API\n\n### spawn(file, arguments?, options?) <sup>default export</sup>\n\n`file`: `string`\\\n`arguments`: `string[]`\\\n`options`: [`Options`](#options)\\\n_Returns_: [`Subprocess`](#subprocess)\n\nExecutes a command using `file ...arguments`.\n\nThis has the same syntax as [`child_process.spawn()`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options).\n\nIf `file` is `'node'`, the current Node.js version and [flags](https://nodejs.org/api/cli.html#options) are inherited.\n\n#### Options\n\n##### 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\n\nAll [`child_process.spawn()` options](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options) can be passed to [`spawn()`](#spawnfile-arguments-options-default-export).\n\n##### options.env\n\n_Type_: `object`\\\n_Default_: `{}`\n\nOverride specific [environment variables](https://en.wikipedia.org/wiki/Environment_variable). Other environment variables are inherited from the current process ([`process.env`](https://nodejs.org/api/process.html#processenv)).\n\n##### options.preferLocal\n\n_Type_: `boolean`\\\n_Default_: `false`\n\nAllows executing binaries installed locally with `npm` (or `yarn`, etc.).\n\n##### options.stdin, options.stdout, options.stderr\n\n_Type_: `string | number | Stream | {string: string}`\n\nSubprocess's standard [input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin))/[output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout))/[error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)).\n\n[All values supported](https://nodejs.org/api/child_process.html#optionsstdio) by `node:child_process` are available. The most common ones are:\n- `'pipe'` (default value): returns the output using [`result.stdout`](#resultstdout), [`result.stderr`](#resultstderr) and [`result.output`](#resultoutput).\n- `'inherit'`: uses the current process's [input](https://nodejs.org/api/process.html#processstdin)/[output](https://nodejs.org/api/process.html#processstdout). This is useful when running in a terminal.\n- `'ignore'`: discards the input/output.\n- [`Stream`](https://nodejs.org/api/stream.html#stream): redirects the input/output from/to a stream. For example, [`fs.createReadStream()`](https://nodejs.org/api/fs.html#fscreatereadstreampath-options)/[`fs.createWriteStream()`](https://nodejs.org/api/fs.html#fscreatewritestreampath-options) can be used, once the stream's [`open`](https://nodejs.org/api/fs.html#event-open) event has been emitted.\n- `{string: '...'}`: passes a string as input to `stdin`.\n\n#### Subprocess\n\nSubprocess started by [`spawn()`](#spawnfile-arguments-options-default-export).\n\n##### await subprocess\n\n_Returns_: [`Result`](#result)\\\n_Throws_: [`SubprocessError`](#subprocesserror)\n\nA subprocess is a promise that is either resolved with a successful [`result` object](#result) or rejected with a [`subprocessError`](#error).\n\n##### subprocess.stdout\n\n_Returns_: `AsyncIterable<string>`\\\n_Throws_: [`SubprocessError`](#subprocesserror)\n\nIterates over each [`stdout`](#resultstdout) line, as soon as it is available.\n\nThe iteration waits for the subprocess to end (even when using [`break`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) or [`return`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)). It throws if the subprocess [fails](#subprocesserror). This means you do not need to call [`await subprocess`](#await-subprocess).\n\n##### subprocess.stderr\n\n_Returns_: `AsyncIterable<string>`\\\n_Throws_: [`SubprocessError`](#subprocesserror)\n\nSame as [`subprocess.stdout`](#subprocessstdout) but for [`stderr`](#resultstderr) instead.\n\n##### subprocess[Symbol.asyncIterator]\\()\n\n_Returns_: `AsyncIterable<string>`\\\n_Throws_: [`SubprocessError`](#subprocesserror)\n\nSame as [`subprocess.stdout`](#subprocessstdout) but for both [`stdout` and `stderr`](#resultoutput).\n\n##### subprocess.pipe(file, arguments?, options?)\n\n`file`: `string`\\\n`arguments`: `string[]`\\\n`options`: [`Options`](#options)\\\n_Returns_: [`Subprocess`](#subprocess)\n\nSimilar to the `|` symbol in shells. [Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the subprocess's[`stdout`](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)) to a second subprocess's [`stdin`](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)).\n\nThis resolves with that second subprocess's [result](#result). If either subprocess is rejected, this is rejected with that subprocess's [error](#subprocesserror) instead.\n\nThis follows the same syntax as [`spawn(file, arguments?, options?)`](#spawnfile-arguments-options-default-export). It can be done multiple times in a row.\n\n##### await subprocess.nodeChildProcess\n\n_Type_: `ChildProcess`\n\nUnderlying [Node.js child process](https://nodejs.org/api/child_process.html#class-childprocess).\n\nAmong other things, this can be used to terminate the subprocess using [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) or exchange IPC message using [`.send()`](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback).\n\n#### Result\n\nWhen the subprocess succeeds, its [promise](#await-subprocess) is resolved with an object with the following properties.\n\n##### result.stdout\n\n_Type_: `string`\n\nThe output of the subprocess on [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)).\n\nIf the output ends with a [newline](https://en.wikipedia.org/wiki/Newline), that newline is automatically stripped.\n\nThis is an empty string if either:\n- The [`stdout`](#optionsstdin-optionsstdout-optionsstderr) option is set to another value than `'pipe'` (its default value).\n- The output is being iterated using [`subprocess.stdout`](#subprocessstdout) or [`subprocess[Symbol.asyncIterator]`](#subprocesssymbolasynciterator).\n\n##### result.stderr\n\n_Type_: `string`\n\nLike [`result.stdout`](#resultstdout) but for the [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)) instead.\n\n##### result.output\n\n_Type_: `string`\n\nLike [`result.stdout`](#resultstdout) but for both the [standard output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)) and [standard error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)), interleaved.\n\n##### result.command\n\n_Type_: `string`\n\nThe file and arguments that were run.\n\nIt is intended for logging or debugging. Since the escaping is fairly basic, it should not be executed directly.\n\n##### result.durationMs\n\n_Type_: `number`\n\nDuration of the subprocess, in milliseconds.\n\n##### result.pipedFrom\n\n_Type_: `Result | SubprocessError | undefined`\n\nIf [`subprocess.pipe()`](#subprocesspipefile-arguments-options) was used, the [result](#result) or [error](#subprocesserror) of the other subprocess that was piped into this subprocess.\n\n#### SubprocessError\n\n_Type_: `Error`\n\nWhen the subprocess fails, its [promise](#await-subprocess) is rejected with this error.\n\nSubprocesses fail either when their [exit code](#subprocesserrorexitcode) is not `0` or when terminated by a [signal](#subprocesserrorsignalname). Other failure reasons include misspelling the command name or using the [`timeout`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options) option.\n\nSubprocess errors have the same shape as [successful results](#result), with the following additional properties.\n\nThis error class is exported, so you can use `if (error instanceof SubprocessError) { ... }`.\n\n##### subprocessError.exitCode\n\n_Type_: `number | undefined`\n\nThe numeric [exit code](https://en.wikipedia.org/wiki/Exit_status) of the subprocess that was run.\n\nThis is `undefined` when the subprocess could not be started, or when it was terminated by a [signal](#subprocesserrorsignalname).\n\n##### subprocessError.signalName\n\n_Type_: `string | undefined`\n\nThe name of the [signal](https://en.wikipedia.org/wiki/Signal_(IPC)) (like [`SIGTERM`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGTERM)) that terminated the subprocess, sent by either:\n- The current process.\n- Another process. This case is [not supported on Windows](https://nodejs.org/api/process.html#signal-events).\n\nIf a signal terminated the subprocess, this property is defined and included in the [error message](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message). Otherwise it is `undefined`.\n\n##### subprocessError.isCanceled\n\n_Type_: `boolean`\n\nWhether the subprocess was canceled using the [`signal` option](https://nodejs.org/api/child_process.html#child-processspawncommand-args-options).\n\n## Windows support\n\nThis package fixes several cross-platform issues with [`node:child_process`](https://nodejs.org/api/child_process.html). It brings full Windows support for:\n- Node modules binaries (without requiring the [`shell`](https://nodejs.org/api/child_process.html#default-windows-shell) option). This includes running `npm ...` or `yarn ...`.\n- `.cmd`, `.bat`, and other shell files.\n- The [`PATHEXT`](https://wiki.tcl-lang.org/page/PATHEXT) environment variable.\n- Windows-specific [newlines](https://en.wikipedia.org/wiki/Newline#Representation).\n\n## Alternatives\n\n`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.\n\n### Execa\n\n[Execa](https://github.com/sindresorhus/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](#maintainers).\n\nOn one hand, it has a bigger size: [![Install size](https://packagephobia.com/badge?p=execa)](https://packagephobia.com/result?p=execa)\n\nOn the other hand, it provides a bunch of additional features: [scripts](https://github.com/sindresorhus/execa/blob/main/docs/scripts.md), [template string syntax](https://github.com/sindresorhus/execa/blob/main/docs/execution.md#template-string-syntax), [synchronous execution](https://github.com/sindresorhus/execa/blob/main/docs/execution.md#synchronous-execution), [file input/output](https://github.com/sindresorhus/execa/blob/main/docs/output.md#file-output), [binary input/output](https://github.com/sindresorhus/execa/blob/main/docs/binary.md), [advanced piping](https://github.com/sindresorhus/execa/blob/main/docs/pipe.md), [verbose mode](https://github.com/sindresorhus/execa/blob/main/docs/debugging.md#verbose-mode), [graceful](https://github.com/sindresorhus/execa/blob/main/docs/termination.md#graceful-termination) or [forceful termination](https://github.com/sindresorhus/execa/blob/main/docs/termination.md#forceful-termination), [IPC](https://github.com/sindresorhus/execa/blob/main/docs/ipc.md), [shebangs on Windows](https://github.com/sindresorhus/execa/blob/main/docs/windows.md), [and much more](https://github.com/sindresorhus/nano-spawn/issues/14). Also, it is [very widely used](https://github.com/sindresorhus/execa/network/dependents) and [battle-tested](https://github.com/sindresorhus/execa/graphs/contributors).\n\nWe 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.\n\n### `node:child_process`\n\n`nano-spawn` is built on top of the [`node:child_process`](https://nodejs.org/api/child_process.html) core module.\n\nIf you'd prefer avoiding adding any dependency, you may use `node:child_process` directly. However, you might miss the [features](#features) `nano-spawn` provides: [proper error handling](#subprocesserror), [full Windows support](#windows-support), [local binaries](#optionspreferlocal), [piping](#subprocesspipefile-arguments-options), [lines iteration](#subprocesssymbolasynciterator), [interleaved output](#resultoutput), [and more](#features).\n\n```js\nimport {execFile} from 'node:child_process';\nimport {promisify} from 'node:util';\n\nconst pExecFile = promisify(execFile);\n\nconst result = await pExecFile('npm', ['run', 'build']);\n```\n\n## Maintainers\n\n- [Sindre Sorhus](https://github.com/sindresorhus)\n- [@ehmicky](https://github.com/ehmicky)\n\n## Related\n\n- [execa](https://github.com/sindresorhus/execa) - Process execution for humans\n- [yoctocolors](https://github.com/sindresorhus/yoctocolors) - Tiny terminal text coloring\n- [yocto-spinner](https://github.com/sindresorhus/yocto-spinner) - Tiny terminal spinner\n","publish_time":1760141694271,"_cnpm_publish_time":1760141694271,"dist-tags":{"latest":"2.1.0"}}