【问题标题】:Error in require('child_process') function not defined - TypeScript未定义 require('child_process') 函数中的错误 - TypeScript
【发布时间】:2016-08-05 12:27:30
【问题描述】:

得到错误:

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

【问题讨论】:

    标签: node.js typescript gulp


    【解决方案1】:

    尝试要求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()); });
    });
    

    【讨论】:

    • 我正在使用 gulp 来编译这一切,所以如果你能帮助我使用 gulp 任务功能在应用程序中使用它会有所帮助。
    • 同样,我正在使用 gulp 进行构建,并使用 child_process 调用外部可执行文件。我会用一个例子来更新我的答案。不过,您不会在 Angular 2 组件中使用它。
    • 如果可能,请提供带有 gulpfile.js 任务示例的示例。谢谢!
    • @RAMWorld,这个例子已经在我更新的答案中了。请参阅“更新:”之后的示例。该示例调用了dotnet 可执行文件,但您可以将其替换为您需要的任何内容。
    【解决方案2】:

    使用 "" 双引号。您可能会尝试在 CLI 中运行并且很有可能是因为引号。

    【讨论】:

      猜你喜欢
      • 2014-01-03
      • 1970-01-01
      • 2016-08-11
      • 2016-04-05
      • 2017-09-04
      • 2015-04-26
      • 1970-01-01
      • 2016-08-27
      • 2017-12-11
      相关资源
      最近更新 更多