RAM World
RAM World

Reputation: 97

Error in require('child_process') function not defined - TypeScript

Getting error:

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)

and app.component.ts is

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}`);
    });
}

Please help me finding the issue. Is there any import missing or any dependency required for using 'child_process'.

My current dependencies are

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

If this is something for which need to modify gulpfile.ts. Please instruct how 'child_process' will added in gulpfile.

Upvotes: 3

Views: 10133

Answers (2)

Deepanshu Singh
Deepanshu Singh

Reputation: 59

Use "" double quotes.You might try to run in CLI & have high possibility it is because quotes.

Upvotes: 0

Ronald Zarīts
Ronald Zarīts

Reputation: 12699

Try to require child_process instead, e.g.:

const exec = require('child_process').exec;

To be honest, not sure what you're trying to achieve, since it looks like an Angular 2 project, which runs in a browser, but child_process is a built-in Node.js component, which would run on a server environment.

UPDATE:

In the context of a Gulp gulpfile, here's an example how one would use child_process to call an external executable - in this case the 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()); });
});

Upvotes: 4

Related Questions