foreground-child

中文文档
4.0.3发布于 2026/02/07BlueOak-1.0.0TypeScriptESM + CJS近 30 天下载 4.9亿

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

中文使用文档
文档目录4 节
  1. 用户
  2. 洞穴
  3. 用户位 : foreground-child/proxy-signals
  4. 用户位 : foreground-child/watchdog

前景孩子

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

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

用户

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.

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

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

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

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

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

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 导出 从这个包裹.

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

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

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

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

用户位 : foreground-child/watchdog

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

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 共发布 30 个版本,首次发布于 2015/05/15;过去 12 个月发布了 4 个版本。

版本发布日期距今
4.0.3latest2026/02/079 个月前
4.0.22026/02/079 个月前
4.0.12026/02/079 个月前
4.0.02026/02/069 个月前
3.3.12025/02/251.7 年前
3.3.02024/08/082 年前

关于 foreground-child 的常见问题

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 和提交记录判断。