错误需要( 'child_process')没有定义的函数 - 打字稿
问题描述:
四处错误:错误需要( 'child_process')没有定义的函数 - 打字稿
zone.js:260 Uncaught EXCEPTION: Error in ./AppComponent class AppComponent - inline template:8:48
ORIGINAL EXCEPTION: ReferenceError: require is not defined
ORIGINAL STACKTRACE:
ReferenceError: require is not defined
at AppComponent.performCmdOperation (http://localhost:8000/app/app.component.js:43:67)
和 app.component.ts是
export class AppComponent{
SelectType = EnumSelectType;
selectedSection: SelectType = EnumSelectType[EnumSelectType.Home];
selectPage(selectType:EnumSelectType) {
console.log('Global : ' + EnumSelectType[selectType]);
this.selectedSection = EnumSelectType[selectType];
console.log(this.selectedSection + ' selected');
}
performCmdOperation(){
console.log('in perform action');
const exec = require('sdk/system/child_process').exec; //this is the point where I'm getting error.
exec('ipconfig', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
请帮我找出这个问题。使用'child_process'是否需要import missing or any dependency
。
我现在的依赖是
"bootstrap": "^3.3.6",
"@angular/common": "2.0.0-rc.4",
"@angular/compiler": "2.0.0-rc.4",
"@angular/core": "2.0.0-rc.4",
"@angular/http": "2.0.0-rc.4",
"@angular/platform-browser": "2.0.0-rc.4",
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
"@angular/router": "2.0.0-rc.1",
"@angular/router-deprecated": "2.0.0-rc.1",
"@angular/upgrade": "2.0.0-rc.4",
"systemjs": "0.19.27",
"es6-shim": "^0.35.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12"
如果这是它需要修改gulpfile.ts东西。请指导如何在gulpfile中添加“child_process”。
答
尝试要求child_process
代替,例如:
const exec = require('child_process').exec;
说实话,不知道你想要达到的,因为它看起来像一个角2项目,它运行在浏览器中,但child_process
是一个内置的Node.js组件,它可以在服务器环境中运行。
UPDATE:
在咕嘟咕嘟gulpfile的情况下,这里有一个例子人会如何使用child_process
调用外部可执行文件 - 在这种情况下,DOTNET CLI。
var ps = require("child_process");
gulp.task("serve:prod", function() {
const dotnetPs = ps.exec("dotnet run");
// redirect standard output of the child process to the console.
dotnetPs.stdout.on("data", function(data) { console.log(data.toString()); });
dotnetPs.stderr.on("data", function(data) { console.error(data.toString()); });
});
我正在使用gulp来编译这一切,所以如果你可以帮助我使用gulp任务函数在应用程序中使用它,这将有助于。 –
同样在这里,我为我的构建使用gulp,并使用'child_process'来调用外部可执行文件。我会用一个例子更新我的答案。不过,您不会在Angular 2组件中使用它。 –
请尽可能使用gulpfile.js任务示例提供示例。谢谢! –