Search code examples
javascriptnode.jsserverruntime-error

What is the source of this error attempting to read my local html file using Node.js?


This is all happening on my one machine -- both the server and the client machine are the same. I am learning on w3schools and have successfully completed the Node.js examples in order until this one where I am attempting to read an html file to use as content to display. The server starts correctly as seen in the console with the blinking cursor like the prior examples, however upon trying to access localhost through chrome, I get the console logged error as well as a notification in chrome with "127.0.0.1 refused to connect."

error:

node:_http_outgoing:949
    throw new ERR_INVALID_ARG_TYPE(
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
    at write_ (node:_http_outgoing:949:11)
    at ServerResponse.write (node:_http_outgoing:904:15)
    at ReadFileContext.callback (C:\path-to-file-omitted\demo_readfile.js:7:9)
    at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:299:13) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v22.13.0

The two files verbatim as I used them from w3schools.

demofile1.html

<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>

demo_readfile.js

var http = require('http');
var fs = require('fs');



http.createServer(function (req, res) {
  fs.readFile('demofile1.html', function(err, data) { // read the html file
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data); // write the html content to the view
    return res.end();
  });
}).listen(8080);

Using Powershell to start node.js instance with my file using 'node C:\path-to-file\filename.js', I tried running powershell as an admin. I tried doing some searches on google and stack overflow to identify this error elsewhere, and while they appeared similar, the ones I found did not apply directly to my issue as far as I know, nor did they at least give me a resolution.


Solution

  • The code you've provided is indeed correct. The problem is that you use absolute paths which is confusing for the filesystem module.
    To avoid errors like this, you can move your HTML file and JS scripts to one directory and run them using node demo_readfile.js instead of node C:\something\demo_readfile.js. Even better thing to do is use a filesystem path utility module, like this:

    const http = require("http");
    const path = require("path");
    const fs = require("fs");
    
    http.createServer(function (req, res) {
    
      fs.readFile(path.join(__dirname, "demofile1.html"), function(err, data) {
    
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(data);
        return res.end();
    
      });
    
    }).listen(8080);
    

    The above code utilizes path.join() to ensure that fs.readFile() always navigates to the correct path, no matter from where the script is called (which is useful when working with larger projects, eg. when the helper function file is located elsewhere than the index).