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-omitteddemo_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-filefilename.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.
3
Answers
Changing the powershell working directory to the javascript and html file location and starting the server with 'node filename.js' solves the problem instead of using absolute path to start the server. Attributed to David in the comments from the original post.
Because you’re new one so lets break this down:
From your error console, pay attention to the line point to your code:
This means the error is spotted in the line 7, char 9th.
Next, read the error:
So combine both, I think that your
data
is undefined right now and you’re trying to write theundefined
and return to the browser.Then the solutions you should try:
fs.readFile
is your correct path and file name. Your should put 2 files at the same folder.console.log(data)
before the lineres.writeHead...
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 ofnode C:somethingdemo_readfile.js
. Even better thing to do is use a filesystem path utility module, like this:The above code utilizes
path.join()
to ensure thatfs.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).