简体   繁体   中英

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM