cweber105 cweber105 - 2 months ago 21
Node.js Question

Node.js spawn ENOENT error

So I followed this answer and made it to the last step, correctly I think. But then what? I'm trying to run a node file, but it doesn't appear to be in the file listed at the PATH directory. How am I supposed to get it in that folder?

My node entry file:

'use strict';

var express = require("express");
var child_process = require("child_process");

var app = express();


app.get("/go", function(req, res, next) {
var stream = child_process.spawn("node file.js").on("error", function(error) {
console.log("Error!!! : " + error);
throw error;
});
});

app.get("/", function(req, res, next) {
console.log("Hit main page.");
res.end("Got me.");
});

app.listen( (process.env.PORT || 4000), function() {
console.log("Example app listening on Heroku port " + process.env.PORT + " or local port 4000!");
});


And file.js, the file I'm attempting to open:

'use strict';

function sayHello() {
console.log("Hello.");
}

sayHello();

Answer

In your spawn method, you should have:

app.get("/go", function(req, res, next) {
  var stream = child_process.spawn("node", ["file.js"]).on("error", function(error) {
    console.log("Error!!! : " + error);
    throw error;
  });
});