需要带有 TypeScript、SystemJS 和 Electron 的 nodejs“child_process”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36546860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Require nodejs "child_process" with TypeScript, SystemJS and Electron
提问by Ziv Glazer
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:
我正在研究一个简单的 nodejs电子(以前称为原子壳)项目。我正在使用 angular 2 编写它,使用的项目与他们在打字稿文档中推荐的项目设置相同:
tsc:
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"
]
}
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:
我需要运行一个命令,我发现我可以用节点“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;
}
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');
but I couldn't find anyway to add the type information to this var:
但无论如何我都找不到将类型信息添加到这个 var 中:
var child_process : I_CANT_PUT_ANY_CHILD_PROCESS_TYPE_HERE = require('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?
关于如何使用类型信息获取 child_process(或任何其他声明的节点模块,这些模块不是我可以在“:”运算符之后声明的公共接口)的任何想法?
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: ///
正如tenbits所建议的那样,我已将如下引用添加到文件顶部:///
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
并使用了您所说的导入语句,但没有更改我的模块加载器。它仍然没有像预期的那样出现相同的错误。我对改变模块系统感觉不太舒服,因为我的项目使用 angular 2 和他们的文档,他们的一些指南说新项目对这个问题没有以前的偏好(我对模块加载器场景很陌生,我不是完全理解它是如何工作的)。当我尝试更改它时,我遇到了一些关于 angular 2 内容的错误,我目前没有足够的时间进入。难道不应该有办法在不更改模块加载器的情况下做到这一点吗?通过浏览 systemjs 站点,它在开始时说它支持 commonjs 模块: 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
我真的很喜欢一个不会改变模块系统的解决方案,或者更深入地解释正在发生的事情以及解决这些模块加载问题的方法
回答by tenbits
Ok, after some research #L138I have found the solution
好的,经过一些研究#L138我找到了解决方案
You can use import
as before
你可以import
像以前一样使用
import * as child from 'child_process';
var foo: child.ChildProcess = child.exec('foo.sh');
console.log(typeof foo.on);
But you should configure SystemJS
to map the module to NodeJS
.
但是您应该配置SystemJS
为将模块映射到NodeJS
.
System.config({
map: {
'child_process': '@node/child_process'
}
});
That's it!
而已!
回答by rinoy
For me it worked with the callback to display the results.
对我来说,它与回调一起显示结果。
import * as child from 'child_process';
var foo: child.ChildProcess = child.exec('dir', (error: string, stdout: string, stderr: string) => {
console.log(stdout);
});
I didn't add any mappings in SystemJS as I dont have any such configuration in the node application
我没有在 SystemJS 中添加任何映射,因为我在节点应用程序中没有任何这样的配置