出现错误:
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'是否需要任何导入缺失或任何依赖项
。
我当前的依赖项是
"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;
说实话,不确定你想要实现什么,因为它看起来像一个在浏览器中运行的 Angular 2 项目,但 child_process
是一个内置的 Node.js 组件,它将在服务器环境上运行。
更新:
在 Gulp 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()); });
});
关于node.js - require ('child_process' 中的错误)函数未定义 - TypeScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38789271/