0

The fs.readFileSync function does not recognize the HTML-file, even tho it is localted in the same folder. The error says: Error: ENOENT: no such file or directory, open 'node.html'

const http = require('http');
const PORT = 3000;
const fs = require('fs')

const server = http.createServer((req, res) => {
    if (req.url === '/') {
        res.writeHead(200, {'content-type': 'text/html'});
        const homePageHTML = fs.readFileSync('node.html');
        console.log(homePageHTML)
        res.end()
    } else {
        res.writeHead(404, {'content-type': 'text/html'});
        res.write('<h1>Sorry, you were misled!</h1>')
        res.end()
    }
})

server.listen(PORT);
  • 1
    The error message is quite clear, it's unable to find that file in the current working directory. You should check the current working directory and adjust the path to `node.html` – Asesh Jul 02 '22 at 07:23
  • solved It by changing the path to `${__dirname}\\node.html`. – nicht verfügbar Jul 02 '22 at 07:28

3 Answers3

1

I solved It by changing the path to ${__dirname}\node.html. Somehow needs the actual path. Which doesn't seems to be the case in the tutorial I'm watching.

1

The logical explanation for your issue is that when you start your program the current working directory in the OS is not the directory that contains your node.html file. Thus, fs.readFileSync('node.html', "utf8"); which only looks in the current working directory does not find the target file.

As you discovered, you can fix the problem by building a more complete path that specifies that actual directory you want, not just the plain filename. You could also make sure that the current working directory was set properly to your server directory before you started your app.

The usual way to build your own path to your server directory is by using path.join():

fs.readFileSync(path.join(__dirname, 'node.html'), "utf8");
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I didn't know about path.join(). But I'll start using it. Seems like it's working on multiple OSs. :) Thank you. – nicht verfügbar Jul 03 '22 at 18:11
  • 1
    @nichtverfügbar - The nice thing about `path.join()` is that it automatically inserts one and only one path separator between the pieces you have, regardless of whether each piece already had one or not and it uses the appropriate separator for the OS. – jfriend00 Jul 03 '22 at 18:18
  • Thank you so much. This helps me in my work-related projects. I struggle a lot with the appropriate seperation of Win and Linux. :) – nicht verfügbar Jul 03 '22 at 21:10
0

Sometimes trying a basic npm reinstall can help:

npm install

I also noticed another problem when trying to reproduce your problem. You didn't specify and encoding type in your fs.ReadFileSync method, so the console.log outputted a buffer rather than a string. changing your homePageHTML string line into this should work

const homePageHTML = fs.readFileSync('node.html', "utf8");

Let me know if this works or if you have any other problems.

Skyfall106
  • 67
  • 6