# foreground-child 中文文档

规范地址：https://npmcn.com/package/foreground-child/

版本：4\.0\.3
npm 元数据更新时间：2026-02-06T18:01:11.729Z
中文译文更新时间：2026-11-05T14:55:16.810Z
软件包许可：BlueOak-1\.0\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/foreground-child
原文对照：https://npmcn.com/package/foreground-child/?lang=original
项目主页：https://github.com/tapjs/foreground-child#readme
源码仓库：https://github.com/tapjs/foreground-child

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：73 kB，37 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=16
- 运行依赖：1 个
- 首次发布：2015-05-15T06:23:22.908Z
- 最新版本发布：2026-02-06T18:01:11.616Z
- 近 30 天下载量：489802848（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：30（近 12 个月 4 个）

## 中文简介

运行一童子如来前行相. 给它stdio。 退出时退出.

## 安装

```sh
npm install foreground-child
```

也可以使用：`pnpm add foreground-child`、`yarn add foreground-child`、`bun add foreground-child`

## README

# 前景孩子

运行一童子如来前行相. 给它stdio。 退出 当它离开.

主要是这个模块是用来支持 周围的一些案件 将儿童流程包成测试范围。 不过 也可以在您想要一个程序执行时普遍有用 另一段仿佛是“主要”过程,例如,如果一个程序 使用一个 `--cmd` 参数以某种方式执行.

## 用户

```js
import { foregroundChild } from 'foreground-child'
// hybrid module, this also works:
// const { foregroundChild } = require('foreground-child')

// cats out this file
const child = foregroundChild('cat', [__filename])

// At this point, it's best to just do nothing else.
// return or whatever.
// If the child gets a signal, or just exits, then this
// parent process will exit in the same way.
```

您可以通过在之后传递对象提供自定义的产卵选项 程序和参数:

```js
const child = foregroundChild(`cat ${__filename}`, { shell: true })
```

如果您想要执行 在您的前景孩子退出前动作 :

```js
const child = foregroundChild('cat', [__filename], spawnOptions, () => {
  doSomeActions()
})
```

回调可以返回一个承诺来履行 一个同步动作。 如果召回不回一个承诺, 然后它必须在 JavaScript 中完成行动 勾选.

```js
const child = foregroundChild('cat', [__filename], async () => {
  await doSomeAsyncActions()
})
```

如果召回投出或拒绝,那么它就会被解开 节点会出错 .

如果回调值返回字符串值,则该值将作为 退出父进程的信号。 如果它返回一个数字, 然后,该数字将作为父退出状态代码。 若为 返回布尔值 `false`,则父进程将不会是 终止。 如果它回来了 `undefined`,然后它会离开 与 和孩子的过程相同.

## 洞穴

"正常"标准 IO 文件描述符 (0, 1和 Stdin, 2), stdout和stderr分别与儿童过程共享. 此外,如果在父母中设置了IPC频道,那么 信件在文件描述符 3 中被代理给孩子 .

在节点中,也可以绘制任意文件描述符 进入一个儿童过程。 在这种情况下,未来孩子不会 将文件描述符映射到孩子中。 如果文件描述符 0, 1或2用于IPC频道,然后奇怪的行为可能 发生( 例如将 IPC 信件打印到 stderr) .

注意 SIGKILLL 总是会杀死母进程, 但是 将不会代理给孩子进程的信号, 因为 SIGKILLL 无法被抓住。 为了解决这个问题,一个特殊的"监视" 产出一个孩子的过程, 将发送一个SIGKILLL给孩子 进程,如果该程序在 监督员由于父母终止而收到SIGHUP.

在 Windows 上,发布一个 `process.kill(process.pid, signal)` 带有 致命的终止信号可能导致进程以 `1` 状态代码而不是正确报告信号。 这个 模块尝试做正确的事情,但是在Windows系统中,您 可能会看到错误的结果。 据我所知没有 为这个工作.

## 用户位 : `foreground-child/proxy-signals`

如果您只是想要代理给一个孩子过程的信号 主进程接收, 您可以使用 `proxy-signals` 导出 从这个包裹.

```js
import { proxySignals } from 'foreground-child/proxy-signals'

const childProcess = spawn('command', ['some', 'args'])
proxySignals(childProcess)
```

现在,任何致命的信号 通过当前进程收到 亲接儿童过程.

它不会朝另一个方向走; i,信号发送到 儿童过程不会影响父母。 为了这个,听 儿童 `exit` 或者说 `close` 并妥善处理.

## 用户位 : `foreground-child/watchdog`

如果你正在产下一个孩子的过程, 想确保它 如果父进程退出, 您可以使用 此模块导出的监督工具 .

```js
import { watchdog } from 'foreground-child/watchdog'

const childProcess = spawn('command', ['some', 'args'])
const watchdogProcess = watchdog(childProcess)

// watchdogProcess is a reference to the process monitoring the
// parent and child. There's usually no reason to do anything
// with it, as it's silent and will terminate
// automatically when it's no longer needed.
```

## 常见问题

### foreground-child 是什么？

运行一童子如来前行相\. 给它stdio。 退出时退出\.

### 如何安装 foreground-child？

在项目目录运行 npm install foreground-child，或使用 pnpm add / yarn add / bun add。

### foreground-child 支持 TypeScript 吗？

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

### foreground-child 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### foreground-child 需要什么版本的 Node\.js？

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

### foreground-child 可以商用吗？

foreground-child 使用 BlueOak-1\.0\.0 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### foreground-child 的下载量有多少？

根据 npm 官方下载统计，foreground-child 在 2026/10/07 – 2026/11/05 这 30 天内被下载 489,802,848 次，日均约 16,326,762 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### foreground-child 还在维护吗？

最新版本 4\.0\.3 发布于 2026/02/07（9 个月前）；至今共发布 30 个版本，其中过去 12 个月发布了 4 个；首次发布于 2015/05/15。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

