Require nodejs “child_process” with TypeScript, SystemJS and Electron
我正在研究一个简单的nodejs电子(以前称为原子壳)项目。
我使用angular 2编写它,使用与打字稿文档中建议的项目设置相同的项目:
tsc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | { "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"来执行该命令。
无论如何,当我从node.d.ts文件中使用其类型时,找不到"导入"或"要求"它的方法。我在node.d.ts文件中找到了适合我需要的" child_process"界面,
这是在node.d.ts文件中的外观:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 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; } |
但我只能(据我所知)只能通过使用import获得此类型:
1 | import * as child_process from 'child_process'; |
唯一的问题是,当我这样做时,我的应用无法加载,并且在控制台中出现以下错误:
1 | GET file:///C:/angular2Samples/NGW-electron-VS%20-%20TEMP/child_process net::ERR_FILE_NOT_FOUND |
就目前而言,即时通讯使用以下方法解决:
1 | var child_process = require('child_process'); |
但是我仍然找不到将类型信息添加到此var的方法:
1 | var child_process : I_CANT_PUT_ANY_CHILD_PROCESS_TYPE_HERE = require('child_process'); |
关于如何获取类型信息的child_process(或任何其他声明的节点模块,这些模块无法在":"运算符之后声明的公共接口)有什么想法?
非常感谢您的帮助和解释:)
更新------------------------------------------------- -----------------
正如提示所暗示的那样,我在文件顶部添加了以下引用:
///
并使用了您说的导入语句,但没有修改我的模块加载器。它仍然没有按预期出现相同的错误。
我对更改模块系统感到不太自在,因为我的项目使用的是angular 2及其文档和一些指南,他们说新项目没有以前喜欢这个问题(我对模块加载器领域非常陌生,完全了解它的工作原理)。
当我尝试更改它时,我遇到了有关角度2的东西的一些错误,目前我没有足够的时间进行讨论。不更改模块加载程序就应该有办法吗?通过浏览一下systemjs网站,它一开始就说它支持commonjs模块:
System.js文档
我真的会提出一个不会改变模块系统的解决方案,或者更深入地说明正在发生的事情以及解决此类模块加载问题的方法
好吧,经过一番研究#L138我找到了解决方案
您可以像以前一样使用
1 2 3 4 | import * as child from 'child_process'; var foo: child.ChildProcess = child.exec('foo.sh'); console.log(typeof foo.on); |
但是您应该配置
1 2 3 4 5 | System.config({ map: { 'child_process': '@node/child_process' } }); |
而已!
对我来说,它与回调一起显示结果。
1 2 3 4 5 | import * as child from 'child_process'; var foo: child.ChildProcess = child.exec('dir', (error: string, stdout: string, stderr: string) => { console.log(stdout); }); |
我没有在SystemJS中添加任何映射,因为在节点应用程序中没有任何此类配置