Node.js产生ENOENT错误

时间:2016-10-06 22:42:56

标签: node.js child-process

所以我跟着this answer进入了最后一步,我认为正确。但那又怎样?我正在尝试运行节点文件,但它似乎不在PATH目录中列出的文件中。我该如何在该文件夹中获取它?

我的节点输入文件:

'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!");
});

和file.js,我正试图打开的文件:

'use strict';

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

sayHello();

1 个答案:

答案 0 :(得分:3)

在你的spawn方法中,你应该有:

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