tinyexec

中文文档
1.3.1发布于 2026/09/03MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 6.7亿

A minimal library for executing processes in Node

原始使用文档
文档目录19 节
  1. Installing
  2. Usage
  3. Options
  4. Passing a string to stdin
  5. Piping to another process
  6. Killing a process
  7. Node modules/binaries
  8. Using an abort signal
  9. Using with command strings
  10. Synchronous
  11. API
  12. pipe(command[, args[, options]])
  13. process
  14. kill([signal])
  15. pid
  16. aborted
  17. killed
  18. exitCode
  19. Comparison with other libraries

tinyexec 📟

A minimal package for executing commands

This package was created to provide a minimal way of interacting with child processes without having to manually deal with streams, piping, etc.

Installing

$ npm i -S tinyexec

Usage

A process can be spawned and awaited like so:

import {x} from 'tinyexec';

const result = await x('ls', ['-l']);

// result.stdout - the stdout as a string
// result.stderr - the stderr as a string
// result.exitCode - the process exit code as a number

By default, tinyexec does not throw on non‑zero exit codes. Check result.exitCode or pass {throwOnError: true}.

Output is returned exactly as produced; trailing newlines are not trimmed. If you need trimming, do it explicitly:

const clean = result.stdout.replace(/\r?\n$/, '');

You may also iterate over the lines of output via an async loop:

import {x} from 'tinyexec';

const proc = x('ls', ['-l']);

for await (const line of proc) {
  // line will be from stderr/stdout in the order you'd see it in a term
}

Options

Options can be passed to have finer control over spawning of the process:

await x('ls', [], {
  timeout: 1000
});

The options object can have the following properties:

  • signal - an AbortSignal to allow aborting of the execution
  • timeout - time in milliseconds at which the process will be forcibly killed
  • persist - if true, the process will continue after the host exits
  • stdin - string or another Result that will be used as the input to the process
  • nodeOptions - any valid options to node's underlying spawn function
  • throwOnError - if true, non-zero exit codes will throw an error
  • nodePath - if false, node_modules/.bin directories and the current node executable's directory will not be prepended to PATH (defaults to true)

Passing a string to stdin

You can pass a string to stdin, which is useful for whitespace-sensitive values and for secrets that shouldn’t be exposed in shell history:

const result = await x('gh', ['auth', 'login', '--with-token'], {
  stdin: process.env.GITHUB_TOKEN
});

console.log(result.exitCode);

Piping to another process

You can pipe a process to another via the pipe method:

const proc1 = x('ls', ['-l']);
const proc2 = proc1.pipe('grep', ['.js']);
const result = await proc2;

console.log(result.stdout);

pipe takes the same options as a regular execution. For example, you can pass a timeout to the pipe call:

proc1.pipe('grep', ['.js'], {
  timeout: 2000
});

Killing a process

You can kill the process via the kill method:

const proc = x('ls');

proc.kill();

// or with a signal
proc.kill('SIGHUP');

Node modules/binaries

By default, node's available binaries from node_modules will be accessible in your command.

For example, in a repo which has eslint installed:

await x('eslint', ['.']);

In this example, eslint will come from the locally installed node_modules.

If you'd rather not have node_modules/.bin (or the directory of the current node executable) prepended to PATH, pass nodePath: false:

await x('eslint', ['.'], {nodePath: false});

Using an abort signal

An abort signal can be passed to a process in order to abort it at a later time. This will result in the process being killed and aborted being set to true.

const aborter = new AbortController();
const proc = x('node', ['./foo.mjs'], {
  signal: aborter.signal
});

// elsewhere...
aborter.abort();

await proc;

proc.aborted; // true
proc.killed; // true

Using with command strings

If you need to continue supporting commands as strings (e.g. "command arg0 arg1"), you can use args-tokenizer, a lightweight library for parsing shell command strings into an array.

import {x} from 'tinyexec';
import {tokenizeArgs} from 'args-tokenizer';

const commandString = 'echo "Hello, World!"';
const [command, ...args] = tokenizeArgs(commandString);
const result = await x(command, args);

result.stdout; // Hello, World!

Synchronous

You can use xSync for synchronous (blocking) execution:

import {xSync} from 'tinyexec';

const result = xSync('ls', ['-l']);

// result.stdout - the stdout as a string
// result.stderr - the stderr as a string
// result.exitCode - the process exit code as a number

Like the async API, you can iterate over lines:

const result = xSync('ls', ['-l']);

for (const line of result) {
  // line will be from stdout then stderr
}

Since the synchronous API blocks the event loop, there are some features that are supported in the async API that the sync API does not support:

  • signal
  • persist
  • kill() method
  • stdin piping
  • pipe() method

Other options like timeout, throwOnError, and nodeOptions work the same way.

API

Calling x(command[, args]) returns an awaitable Result which has the following API methods and properties available:

pipe(command[, args[, options]])

Pipes the current command to another. For example:

x('ls', ['-l'])
  .pipe('grep', ['js']);

The parameters are as follows:

  • command - the command to execute (without any arguments)
  • args - an array of arguments
  • options - options object

process

The underlying Node.js ChildProcess. tinyexec keeps the surface minimal and does not re‑expose every child_process method/event. Use proc.process for advanced access (streams, events, etc.).

const proc = x('node', ['./foo.mjs']);

proc.process?.stdout?.on('data', (chunk) => {
  // ...
});
proc.process?.once('close', (code) => {
  // ...
});

kill([signal])

Kills the current process with the specified signal. By default, this will use the SIGTERM signal.

For example:

const proc = x('ls');

proc.kill();

pid

The current process ID. For example:

const proc = x('ls');

proc.pid; // number

aborted

Whether the process has been aborted or not (via the signal originally passed in the options object).

For example:

const proc = x('ls');

proc.aborted; // bool

killed

Whether the process has been killed or not (e.g. via kill() or an abort signal).

For example:

const proc = x('ls');

proc.killed; // bool

exitCode

The exit code received when the process completed execution.

For example:

const proc = x('ls');

proc.exitCode; // number (e.g. 1)

Comparison with other libraries

tinyexec aims to provide a lightweight layer on top of Node's own child_process API.

Some clear benefits compared to other libraries are that tinyexec will be much lighter, have a much smaller footprint and will have a less abstract interface (less "magic"). It will also have equal security and cross-platform support to popular alternatives.

There are various features other libraries include which we are unlikely to ever implement, as they would prevent us from providing a lightweight layer.

For example, if you'd like write scripts rather than individual commands, and prefer to use templating, we'd definitely recommend zx. zx is a much higher level library which does some of the same work tinyexec does but behind a template string interface.

Similarly, libraries like execa will provide helpers for various things like passing files as input to processes. We opt not to support features like this since many of them are easy to do yourself (using Node's own APIs).

版本与发布记录

tinyexec 共发布 26 个版本,首次发布于 2024/07/02;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.1latest2026/09/032 个月前
1.3.02026/08/013 个月前
1.2.42026/05/315 个月前
1.2.32026/05/295 个月前
1.2.22026/05/245 个月前
1.2.12026/05/235 个月前

其他发布通道:next → 1.1.2-next.0。

关于 tinyexec 的常见问题

tinyexec 是什么?

A minimal library for executing processes in Node

如何安装 tinyexec?

在项目目录运行 npm install tinyexec,或使用 pnpm add / yarn add / bun add。

tinyexec 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

tinyexec 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

tinyexec 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

tinyexec 可以商用吗?

tinyexec 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

tinyexec 的下载量有多少?

根据 npm 官方下载统计,tinyexec 在 2026/10/05 – 2026/11/03 这 30 天内被下载 665,890,491 次,日均约 22,196,350 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

tinyexec 还在维护吗?

最新版本 1.3.1 发布于 2026/09/03(2 个月前);至今共发布 26 个版本,其中过去 12 个月发布了 12 个;首次发布于 2024/07/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。