Home > Software design >  TypeError [ERR_INVALID_CALLBACK]: On NodeJs
TypeError [ERR_INVALID_CALLBACK]: On NodeJs

Time:10-09

Received undefined throw new ERR_INVALID_CALLBACK(cb);

const fs = require("fs");
const text = "File ";

fs.writeFile("node-message.txt", text)
  .then(() => {
    console.log("File Created");
  })
  .catch(() => {
    console.log("File not created");
  });

Iam getting this TypeError [ERR_INVALID_CALLBACK]: Callback must be a function.

CodePudding user response:

You have to require an fs module with promises if you are facing an invalid callback type error.

    const fs = require("fs").promises;
    const text = "File ";
    
    fs.writeFile("node-message.txt", text)
      .then(() => {
        console.log("File Created");
      })
      .catch(() => {
        console.log("File not created");
      });
  • Related