Node.js “需要节点”;儿童“过程”;使用TypeScript、SystemJS和Electron

Node.js “需要节点”;儿童“过程”;使用TypeScript、SystemJS和Electron,node.js,typescript,require,electron,systemjs,Node.js,Typescript,Require,Electron,Systemjs,我正在从事一个简单的nodejs(以前称为atomshell)项目。 我使用angular 2编写它,使用与typescript文档中推荐的相同的项目设置: 贸易与供应链: 我需要运行一个命令,我发现我可以用节点“child_process”来完成。 在node.d.ts文件中使用其类型时,我无论如何都找不到可以“导入”或“需要”的类型。我在node.d.ts文件中找到了适合我需要的“child_process”接口, 这是它在node.d.ts文件中的外观: declare modul

我正在从事一个简单的nodejs(以前称为atomshell)项目。 我使用angular 2编写它,使用与typescript文档中推荐的相同的项目设置:

贸易与供应链:

我需要运行一个命令,我发现我可以用节点“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_进程(或我可以在“operator”之后声明的非公共接口的任何其他声明的节点模块)有何想法

提前非常感谢您的帮助和解释:)

更新------------------------------------------------------------------

根据tenbits的建议,我在文件顶部添加了如下参考: ///

使用了你说的导入语句,但没有更改我的模块加载器。它仍然没有像预期的那样以同样的错误工作。 我对改变模块系统感觉不太舒服,因为我的项目使用angular 2,他们的文档和一些指导人员说新项目以前不喜欢这个问题(我对模块加载程序场景非常陌生,我还不完全了解它是如何工作的)。 当我试图改变它,我得到了一些错误的角度2的东西,我没有足够的时间进入目前。在不改变模块加载器的情况下,是否应该有一种方法来实现这一点?通过浏览systemjs站点,它一开始就表示它支持commonjs模块:

我真的会推荐一个不会改变模块系统的解决方案,或者更深入地解释正在发生的事情以及解决此类模块加载问题的方法。

好的,经过一些研究,我找到了解决方案

您可以像以前一样使用
import

import * as child from 'child_process';

var foo: child.ChildProcess = child.exec('foo.sh');
console.log(typeof foo.on);
但是您应该配置
SystemJS
将模块映射到
NodeJS

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

就这样

对我来说,它与回调一起显示结果

import * as child from 'child_process';

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

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

如果错误消息是“找不到模块‘child_process’或其相应的类型声明”答案是“npm install@types/watch”非常感谢您的评论!我已经试过了,请看我对关于你答案的问题的更新:)我不太明白:你试过
import child=require('child_process')了吗而不是
var child=require('child_process')?您是否有与
var
相同的结果?是的,完全相同(请注意,我没有按照您的建议将模块更改为commonjs,原因已在上面的更新中说明)再次感谢您的新建议。恐怕不行。我复制了您上一个版本的System.config,并将代码更改为使用带有require的import语句,正如您在第一个版本的andwer中建议的那样,但仍然得到了相同的xhr错误。我也尝试过:从“child_进程”导入*作为子对象;同样的错误也不起作用。我还尝试使用node.d.ts顶部的///引用来实现它,但正如我预期的那样,它没有改变任何东西。很高兴它很有用,实际上我不熟悉
SystemJS
,只熟悉TypeScript和Electron,但我想问题是,
SystemJS
作为常规浏览器依赖项加载nodejs模块。所以首先我建议以某种方式将其映射到CommonJS模块名模式。当这一点没有帮助时,我只是查看源代码以了解映射的实际工作原理,并发现这是需要的
@node
前缀。是的,应该有文档记录,但您可以随时查看源代码,以便了解库是如何“设计”的。干杯
import * as child from 'child_process';

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