Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Need help when following along on local Windows machine when using readFileSync

I'm on a local machine on Windows.

When I try to launch my node site to pull in header.html from ./views/header.html I get the following error:

Error: ENOENT: no such file or directory, open 'C:\Users<REDACTED USERNAME\Documents\code\nodeSite\js\views\header.html' at Object.openSync (fs.js:457:3) at Object.readFileSync (fs.js:359:35) at Object.view (C:\Users\REDACTEDUSERNAME\Documents\code\nodeSite\js\renderer.js:5:27) at Object.home (C:\Users\REDACTEDUSERNAME\Documents\code\nodeSite\js\router.js:7:16) at Server.<anonymous> (C:\Users\REDACTEDUSERNAME\Documents\code\nodeSite\js\app.js:11:10)
at Server.emit (events.js:311:20) at parserOnIncoming (_http_server.js:784:12) at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17) { errno: -4058, syscall: 'open', code: 'ENOENT', path: 'C:\Users\REDACTEDUSERNAME\Documents\code\nodeSite\js\views\header.html' } PS C:\Users\REDACTEDUSERNAME\Documents\code\nodeSite\js>

username in the paths are removed in this question.

The code i have in my renderer.js file is

const fs = require('fs'); const path = require('path');

function view(templateName, values, response) { let fileContents = fs.readFileSync(path.resolve(./views/${templateName}.html)); response.write(fileContents);


module.exports.view = view;

I've been racking my brains on this trying to figure out why the code refuses to work.

1 Answer

James Crosslin
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,758 Points
function view(templateName, values, response) { 
    let fileContents = fs.readFileSync(path.resolve(./views/${templateName}.html)); 

There are no backticks around the path. It should look like this