需要nodejs"child_process";使用TypeScript,SystemJS和Electron [英] Require nodejs "child_process" with TypeScript, SystemJS and Electron

查看:123
本文介绍了需要nodejs"child_process";使用TypeScript,SystemJS和Electron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个简单的nodejs 电子(以前称为原子壳)项目. 我使用angular 2编写它,使用与打字稿文档中建议的项目设置相同的项目设置:

I'm working on a simple nodejs electron (formerly known as atom shell) project. I'm writing it using angular 2, using the project the same project setup as they recommend in the documentation for 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文件中的外观:

I need to run a command, I found out that I can do it with node "child_process". I couldn't find anyway for me to "import" or "require" it while having its type used from the node.d.ts file. I have found the "child_process" interface in the node.d.ts file which suits my need, this is how it looking in the node.d.ts file:

    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来获得这种类型:

but I can only (as I know of) get this type only by using import:

import * as child_process from 'child_process'; 

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

Only problem is that when I do this, my app cant load and I get the following error in the console:

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

目前,我通过使用以下方法来解决问题:

For now, im getting my way around by using:

var child_process = require('child_process');

但是我仍然找不到将类型信息添加到此var的信息:

but I couldn't find anyway to add the type information to this var:

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

关于如何获取类型类型信息的child_process(或其他可以声明为:"运算符的公共接口的声明的节点模块)的想法吗?

Any ideas on how I can get the child_process (or any other declared node modules that arent public interface that I can state after ":" operator) with type information?

在此先感谢您的帮助和说明:)

Thanks alot in advance for any help and explanations :)

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

UPDATE ------------------------------------------------------------------

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

As tenbits suggested I have added the reference as follows to the top of the file: ///

并使用了您说的导入语句,但没有修改我的模块加载器.它仍然无法正常工作,并且出现了与预期相同的错误. 我对更改模块系统感到不太自在,因为我的项目使用的是angular 2及其文档和一些指南,他们说以前没有优先考虑此事的新项目(我对模块加载器领域非常陌生,完全了解它的工作原理). 当我尝试更改它时,我遇到了有关角度2的东西的一些错误,目前我没有足够的时间进行讨论.不更改模块加载器就没有办法做到这一点吗?通过浏览systemjs网站,它一开始就说它支持commonjs模块: Systemjs文档

and used the import statment you said, but didnt chage my module loader. it still didnt work with the same error as expected. Im not feeling very comfortable about changing the module system, as my project uses angular 2 and their docs and some of their guides said that new projects that has no former prefernce to this matter (I am very new to the module loaders scene and im not fully understanding how it works yet). When I tried to change it I got some errors regarding angular 2 stuffs which I dont have enough time to get into at the moment. Shouldn't there be a way to this without changing the module loader? by glancing at the systemjs site it says at the start that it supports commonjs modules: Systemjs doc

我真的会提出一个不会改变模块系统的解决方案,或者更深入地说明正在发生的事情以及那里存在解决此类模块加载问题的方法的方法

I would really appriciate a solution that doesn't change the module system, or maybe a more depth explanition about what's going on and which approaches to these kind of module loading problems exists out there

推荐答案

好,经过一些研究

Ok, after some research #L138 I have found the solution

您可以像以前一样使用import

You can use import as before

import * as child from 'child_process';

var foo: child.ChildProcess = child.exec('foo.sh');
console.log(typeof foo.on);

但是您应该配置SystemJS以将模块映射到NodeJS.

But you should configure SystemJS to map the module to NodeJS.

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

就是这样!

这篇关于需要nodejs"child_process";使用TypeScript,SystemJS和Electron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆