我正在研究一個簡單的nodejs electron(以前稱爲原子shell)項目。 我使用角2手寫它,使用與項目相同的項目設置,因爲他們的文檔中推薦打字稿:要求使用TypeScript,SystemJS和Electron的nodejs「child_process」
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」來完成。 無論如何我無法找到「導入」或「需要」它,而從node.d.ts文件中使用它的類型。我發現在node.d.ts文件中的「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;
}
但我只能(如我知道的),只能通過進口獲得此類型:
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');
,但我找不到反正添加的類型信息,該變種:
var child_process : I_CANT_PUT_ANY_CHILD_PROCESS_TYPE_HERE = require('child_process');
我如何能得到child_process任何想法(或任何其他聲明的節點不能公開接口的模塊,我可以在「:」運算符之後聲明類型信息?
非常感謝提前任何幫助和解釋:)
UPDATE ------------------------------ ------------------------------------
由於tenbits建議我添加了如下參考到該文件的頂部: ///
並使用您所說的導入聲明,但沒有chage我的模塊加載程序。它仍然沒有像預期的那樣工作。 對於改變模塊系統我感覺不太舒服,因爲我的項目使用了角度2,他們的文檔和他們的一些指南說,沒有前者喜歡這個問題的新項目(我對模塊加載器現場和IM不完全理解它是如何工作的)。 當我試圖改變它時,我得到了一些有關角2東西的錯誤,目前我沒有足夠的時間進入。如果不改變模塊加載器,不應該有辦法嗎?通過在systemjs現場一瞧,它說在一開始,它支持CommonJS的模塊: Systemjs doc
我真的並欣賞不改變的模塊系統,也許這是怎麼回事就和一個更深入explanition的解決方案接近這些類型的模塊加載問題存在那裏
非常感謝您的評論任何這樣的配置!我試過了,請看看我更新的問題關於你的答案:) –
我不太明白它:你有沒有試過'import child = require('child_process');'而不是'var child = require ( 'child_process');'?你和'var'有相同的結果嗎? – tenbits
是的,完全一樣(請注意,我沒有按照您的建議將模塊更改爲commonjs,其原因在上面的更新中註明) –