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

问题描述

我正在开发一个简单的 nodejs electron(以前称为 atom shell)项目.我正在使用 angular 2 编写它,使用与他们在 typescript 文档中推荐的相同项目设置:

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 ------------------------------------------------------------------

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

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

并使用了您所说的导入语句,但没有更改我的模块加载器.它仍然没有像预期的那样出现相同的错误.我对更改模块系统感觉不太舒服,因为我的项目使用 angular 2,他们的文档和他们的一些指南说新项目没有以前对这个问题的偏好(我对模块加载器场景很陌生,我不是完全理解它是如何工作的).当我尝试更改它时,我遇到了一些关于 angular 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

推荐答案

好的,经过一番研究#L138 我找到了解决办法

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

你可以像以前一样使用import

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 05:46
查看更多