JavaScript Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Reading from Files

Bryan LeBlanc
Bryan LeBlanc
2,482 Points

error: no such file or directory, open

When I try to run app.js I keep getting the same error after I refresh the page after I spooling up the server.

"ENOENT: no such file or directory, open 'header.html' events.js:163 throw er; // Unhandled 'error' event ^

Error: write after end at ServerResponse.write (_http_outgoing.js:485:15) at ReadFileContext.callback (/Applications/MAMP/htdocs/Node-Project/build/js/renderer.js:12:14) at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:359:13) "

I know the file exists.

Anyone else have this problem or have a solution?

Bryan LeBlanc
Bryan LeBlanc
2,482 Points

I figured it out. You just need to include the entire file path. I'm doing this on my machine so my path looked like this:

let fileContents = fs.readFileSync('/Applications/MAMP/htdocs/Node-Project/views/' + templateName + '.html');

Andres Ramirez
Andres Ramirez
17,239 Points

@ Bryan LeBlanc your solution works! Thank you so much!!

I was about to slam my computer against the wall :P

3 Answers

Rebekah Shaw
Rebekah Shaw
16,696 Points

I am having the same problem but I am using workspaces, so confused

Also having this problem in workspace here is my renderer.js:

var fs = require("fs");

function view(templateName, values, response){
  //Read from the template files
  var fileContents = fs.readFileSync('./views/' + templateName + '.html');

  //Insert values into the content

  //write out to the response
  response.write(fileContents);
  }
module.exports.view = view;
J P
J P
23,728 Points

I had the same problem. Just like Bryan LeBlanc said earlier, the issue is the file path. I used the file path of the open folder I'm using in VS Code which fixed the problem.

My Before: './views/' + templateName + '.html' My After: 'stage_1_video_3/views/' + templateName + '.html'