1

I have two files under a folder with names "app.js" and "child.js". The Node is running on Windows OS. The app.js file:

;(function() {
    var http = require("http"),
    child_process = require("child_process"),
    exec = child_process.exec;

    http.createServer(function(request, response) {
        response.writeHead(200, {"content-type": "text/plain"});
        exec('node child.js', {env: {number: 1234}}, function(error, stdout, stderror) {
            if(error) throw error;
            console.log(stdout);
            console.log(stderror);
        });
        response.write("Hello world!!!");
        response.end();
    }).listen(8000);
    console.log("The server has started listening to the port: 8000");
})();  

The child.js file:

;(function() {
    var envVar = process.env.envVar;
    console.log("Type of envVar: " + typeof envVar);
    console.log("The value of envVar is: " + parseInt(envVar, 10));
})();

I am attempting to execute an external command via "exec" method.
But when I run:

node app.js  

I receive the error:

Command failed: 'node' is not recognized as an internal or external command, operable program or batch file.  

What wrong am I doing here?

7
  • Sounds like you don't have node installed. Commented Mar 22, 2013 at 8:08
  • @Alex: I have the node installed. When I write "node" on my command line, i get the REPL. Commented Mar 22, 2013 at 8:11
  • you should use fork instead of exec to launch node processes. nodejs.org/api/… Commented Mar 22, 2013 at 8:24
  • @generalhenry: If I write exec("dir *.*", function(err, stdout, stderr) {}), it works fine. I am not able to understand why it fails for the situation mentioned above. Commented Mar 22, 2013 at 8:29
  • Maybe node is not in the PATH, try exec("path", function(err, stdout, stderr) {}), and print to the console what's in your path. You can also specifiy the whole path of node, like c:\Program Files\nodejs\node.exe child.js. Commented Mar 22, 2013 at 8:39

1 Answer 1

1

So if you want to exec a command, try this:

var http = require("http"),
    child_process = require("child_process"),
    exec = child_process.exec;
    http.createServer(function(request, response) {
        response.writeHead(200, {"content-type": "text/plain"});
        exec( '"' + process.execPath + '" child.js', {env: {number: 1234}}, function(error, stdout, stderror) {
            if(error) throw error;
            console.log(stdout);
            console.log(stderror);
        });
        response.write("Hello world!!!");
        response.end();
    }).listen(8000);
    console.log("The server has started listening to the port: 8000");

the process.execPath contains the full path of node.exe, the " should be there, because directory names can contain spaces like Program files.

the child process is the same, I just changed process.env.envVar to process.env.number, because you set that in exec options.

var envVar = process.env.number;
    console.log("Type of envVar: " + typeof envVar);
    console.log("The value of envVar is: " + parseInt(envVar, 10));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.