需要带有 TypeScript、SystemJS 和 Electron 的 nodejs "child_process"

新手上路,请多包涵

我正在开发一个简单的 nodejs 电子(以前称为 atom shell)项目。我正在使用 angular 2 编写它,使用项目设置与他们在 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”来做到这一点。在从 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(或任何其他声明的节点模块,这些节点模块不是我可以在“:”运算符之后声明的公共接口)的任何想法?

非常感谢您的任何帮助和解释:)

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

正如tenbits建议的那样,我在文件顶部添加了如下引用:///

并使用了您说的导入语句,但没有更改我的模块加载器。它仍然没有像预期的那样出现相同的错误。我对更改模块系统感觉不太舒服,因为我的项目使用 angular 2,他们的文档和他们的一些指南说新项目以前没有对这个问题的偏好(我对模块加载器场景很陌生,我不是完全理解它是如何工作的)。当我尝试更改它时,我遇到了一些关于 angular 2 东西的错误,我目前没有足够的时间进入。在不改变模块加载器的情况下不应该有办法吗?通过浏览systemjs站点,它在一开始就说它支持commonjs模块: Systemjs doc

我真的很想找到一个不改变模块系统的解决方案,或者更深入地解释正在发生的事情以及存在哪些解决此类模块加载问题的方法

原文由 Ziv Glazer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 929
2 个回答

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

您可以像以前一样使用 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'
  }
});

而已!

原文由 tenbits 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果错误消息是 “找不到模块‘child_process’或其相应的类型声明” ,则答案是 ‘npm install @types/watch’

原文由 Gustavo Hernán Crucianelli 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题