要求使用TypeScript,SystemJS和Electron的nodejs“child_process”

我正在研究一个简单的nodejs 电子 (以前称为primefaces壳)项目。 我正在使用angular 2编写它,使用项目相同的项目设置,他们build议在typescript的文档中:

TSC:

{ "compilerOptions": { "target": "es5", "module": "system", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }, "exclude": [ "node_modules", "typings/main", "typings/main.d.ts" ] } 

我需要运行一个命令,我发现我可以用节点“child_process”来完成。 无论如何我无法find“导入”或“要求”,而从node.d.ts文件中使用它的types。 我在node.d.ts文件中find了适合我需要的“child_process”接口,这就是它在node.d.ts文件中的样子:

  declare module "child_process" { import * as events from "events"; import * as stream from "stream"; export interface ChildProcess extends events.EventEmitter { stdin: stream.Writable; stdout: stream.Readable; stderr: stream.Readable; pid: number; kill(signal?: string): void; send(message: any, sendHandle?: any): void; disconnect(): void; unref(): void; } export function spawn(command: string, args?: string[], options?: { cwd?: string; stdio?: any; custom?: any; env?: any; detached?: boolean; }): ChildProcess; export function exec(command: string, options: { cwd?: string; stdio?: any; customFds?: any; env?: any; encoding?: string; timeout?: number; maxBuffer?: number; killSignal?: string; }, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; export function exec(command: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; export function execFile(file: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; export function execFile(file: string, args?: string[], callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; export function execFile(file: string, args?: string[], options?: { cwd?: string; stdio?: any; customFds?: any; env?: any; encoding?: string; timeout?: number; maxBuffer?: number; killSignal?: string; }, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; export function fork(modulePath: string, args?: string[], options?: { cwd?: string; env?: any; execPath?: string; execArgv?: string[]; silent?: boolean; uid?: number; gid?: number; }): ChildProcess; export function spawnSync(command: string, args?: string[], options?: { cwd?: string; input?: string | Buffer; stdio?: any; env?: any; uid?: number; gid?: number; timeout?: number; maxBuffer?: number; killSignal?: string; encoding?: string; }): { pid: number; output: string[]; stdout: string | Buffer; stderr: string | Buffer; status: number; signal: string; error: Error; }; export function execSync(command: string, options?: { cwd?: string; input?: string|Buffer; stdio?: any; env?: any; uid?: number; gid?: number; timeout?: number; maxBuffer?: number; killSignal?: string; encoding?: string; }): string | Buffer; export function execFileSync(command: string, args?: string[], options?: { cwd?: string; input?: string|Buffer; stdio?: any; env?: any; uid?: number; gid?: number; timeout?: number; maxBuffer?: number; killSignal?: string; encoding?: string; }): string | Buffer; } 

但我只能(因为我知道)得到这种types只有通过使用导入:

 import * as child_process from 'child_process'; 

唯一的问题是,当我这样做,我的应用程序无法加载,并在控制台中出现以下错误:

 GET file:///C:/angular2Samples/NGW-electron-VS%20-%20TEMP/child_process net::ERR_FILE_NOT_FOUND 

现在,即时通过使用:

 var child_process = require('child_process'); 

但我无法find将types信息添加到此var:

 var child_process : I_CANT_PUT_ANY_CHILD_PROCESS_TYPE_HERE = require('child_process'); 

关于如何获得child_process(或任何其他声明的非公共接口的声明节点模块,我可以在“:”运算符之后声明的types信息)的任何想法?

非常感谢提供任何帮助和解释:)

更新————————————————- —————–

由于tenbitsbuild议我已经添加了如下参考文件的顶部:///

并使用您所说的import统计,但没有chage我的模块加载器。 它仍然没有像预期的相同的错误工作。 我对改变模块系统感觉不太舒服,因为我的项目使用了angular度2,他们的文档和他们的一些指南说,没有以前喜欢这个问题的新项目(我对模块加载器现场是非常新的,我不是充分理解它是如何工作的)。 当我试图改变它,我有一些angular度2的东西,目前我没有足够的时间进入一些错误。 如果不改变模块加载器,不应该有办法吗? 通过浏览systemjs站点,它在开始时说它支持commonjs模块: Systemjs doc

我会真正appriciate不改变模块系统的解决scheme,或者更深入的解释发生了什么事情,哪些方法来处理这些模块加载问题

好的,经过一些研究#L138我find了解决办法

您可以像以前一样使用import

 import * as child from 'child_process'; var foo: child.ChildProcess = child.exec('foo.sh'); console.log(typeof foo.on); 

但是您应该configurationSystemJS以将模块映射到SystemJS

 System.config({ map: { 'child_process': '@node/child_process' } }); 

而已!

对我来说,它与callback一起显示结果。

 import * as child from 'child_process'; var foo: child.ChildProcess = child.exec('dir', (error: string, stdout: string, stderr: string) => { console.log(stdout); }); 

我没有在SystemJS中添加任何映射,因为在节点应用程序中我没有任何这样的configuration