Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const fs = require("fs") not working correctly on Node js

This is my first post here so i apologise if i make an error.

I am having an problem when trying to use const fs = require("fs") in Node js. Visual Studio code IDE is not recognising it as being assigned and when i try to use fs.writeFile i am getting an error.

I have already tried looking online but can not find any solution. I am new to coding and would appreciate some help on this.

const http = require("http");
const fs = require("fs");

const server = http.createServer((req, res) => {
  const url= req.url;

  const method = req.method;
  if (url === "/") {
    res.write("<html>");
    res.write("<head><title>Enter Message</title></head>");
    res.write("<body><form action='/message' method='POST'><input type='text' name='message'><button type='submit'>Enter</button></input></form>");
    res.write("</html>");
    return res.end();
  }
  if (url === "/message" && method === "POST") {
    fs.writeFile("message.txt", "DUMMY");
  }
  res.setHeader("content-type", "text/html");
  res.write("<html>");
  res.write("<head><title>My First Page</title></head>");
  res.write("<body><h1>Hello From My Node Js Server!!!!</h1></body>");
  res.write("<html>");
});

I expected the dummy text to be written to the file when i ran the program but i just get the following error.

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
    at maybeCallback (fs.js:128:9)
    at Object.writeFile (fs.js:1158:14)
    at Server.http.createServer (C:\Users\garry\Documents\Eloquent Javascript\Practice\practice.js:15:8)
    at Server.emit (events.js:189:13)
    at parserOnIncoming (_http_server.js:676:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
like image 748
Smoo Avatar asked Oct 26 '25 08:10

Smoo


2 Answers

According to the Node.js documentation, you must provide a callback function to fs.writeFile.

Reference: https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

fs.writeFile is asynchronous so you need a callback function as mentioned in documentation or you can use fs.writeFileSync

like image 125
yoavmmn Avatar answered Oct 28 '25 23:10

yoavmmn


The method signature for writeFile is, fs.writeFile(file, data[, options], callback) meaning it expects 4 parameters, one is optional; options and your last parameter should be a callback function. The signature for the callback is callback(err) meaning the callback function you pass should take first param to handle error if the function writeFile encounters error while writing. If there is no error then err will be null

Sample usage from docs:

fs.writeFile('message.txt', data, (err) => {
  // check if there is error
  if (err) throw err;
  console.log('The file has been saved!');
});

In your call, the method is missing the final callback function parameter fs.writeFile("message.txt", "DUMMY" // , missing); You can pass in your custom function as well like:

function writeSuccess(err) {
  // check if there is error
  if (err) console.error(err);
  else console.log('write success');
}

fs.writeFile("message.txt", "DUMMY", writeSuccess);

Read more about callback in general on MDN

like image 31
1565986223 Avatar answered Oct 28 '25 21:10

1565986223



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!