Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
18,094 Points

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

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

4 Answers

Rebekah Shaw
Rebekah Shaw
18,687 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;

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'

Sean Yu
Sean Yu
4,401 Points

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