我有一个 typescript 项目,而不是首先使用 tsc
,我只是直接通过 ts-node
运行。
在我的代码中,我需要使用 fork()
创建一个子进程。
如果我运行像 child_process.fork('ChildProcess.ts')
这样的代码并且 ChildProcess.ts
包含一些只包含 typescript 的结构(例如:import {}
, export
, ...),那么解释器是 node
,而不是 ts-node
,将会失败。
可能建议使用类似 child_process.exec('node ./node_modules/.bin/ts-node ChildProcess.ts)
的东西,但我真的想要/需要 IPC 通信 channel 当专门使用 fork()
时,在父进程和子进程之间建立。
关于如何实现这一点有什么想法吗?
谢谢!
最佳答案
作为the reference指出,forked 进程中的 execArgv
是从当前进程继承的:
execArgv List of string arguments passed to the executable. Default: process.execArgv.
当入口点运行为:
ts-node index.ts
execArgv
默认为 ts-node
二进制:
[ '...\\ts-node\\dist\\_bin.js' ]
和
child_process.fork('ChildProcess.ts')
也与 ts-node
一起运行。
关于node.js - 使用 ts-node 时通过 fork() 调用子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52569406/