how to fix this error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function


node.js Problem Overview

I am a beginner to the nodejs. When I type the below, the code error occurs like this:

> TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

var fs = require('fs');
fs.readFile('readMe.txt', 'utf8', function (err, data) {
  fs.writeFile('writeMe.txt', data);

node.js Solutions

Solution 1 - node.js

Fs.writeFile() according to the documentation here takes ( file, data[, options]and callback ) params so your code will be like this :

 var fs = require('fs');
 fs.readFile('readMe.txt', 'utf8', function (err, data) {
  fs.writeFile('writeMe.txt', data, function(err, result) {
     if(err) console.log('error', err);

Solution 2 - node.js

fs.writeFile(...) requires a third (or fourth) parameter which is a callback function to be invoked when the operation completes. You should either provide a callback function or use fs.writeFileSync(...)

See node fs docs for more info.

Solution 3 - node.js

Since node 10, it is mandatory to pass a callback on fs.writefile()

Node.js documented the purpose for the change:

You could add an empty callback like this fs.writeFile('writeMe.txt', data, () => {})

Solution 4 - node.js

you also use like this

var file2 =  fs.readFileSync("./Public/n2.jpeg")

Solution 5 - node.js

You simply could use the sync function

var fs = require('fs');
fs.readFileSync('readMe.txt', 'utf8', function (err, data) {
  fs.writeFileSync('writeMe.txt', data);

or use callback function

Solution 6 - node.js

This error hit me in the face when I was doing the following;

var hello = myfunction( callme() );

rather than

var hello = myfunction( callme );

Solution 7 - node.js

var fs = require('fs');

fs.readFile('readme.txt', 'utf8', function(err, data) {
    fs.writeFile('writemeee.txt', data, function(err, result) {

        if (err) console.log('error', err);


Solution 8 - node.js

you can import the fs module from the fs/promises as they are promise-fied version of the modules so we don't need to use the callback function unnecessarily.

import fs from 'fs/promises';

fs.readFileSync('readMe.txt', 'utf8', function (err, data) {
fs.writeFileSync('writeMe.txt', data);`});`

Solution 9 - node.js

try this .I have written the code using Promises.

const {readFile} = require('fs');
const {writeFileSync} = require('fs');
const readText = (path)=>{
 return new Promise((resolve,reject) => {



All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThirupparanView Question on Stackoverflow
Solution 1 - node.jsWejd DAGHFOUSView Answer on Stackoverflow
Solution 2 - node.jsphuziView Answer on Stackoverflow
Solution 3 - node.jsOscar ZhangView Answer on Stackoverflow
Solution 4 - node.jsShashwat GuptaView Answer on Stackoverflow
Solution 5 - node.jsMD SHAYONView Answer on Stackoverflow
Solution 6 - node.jsJim AhoView Answer on Stackoverflow
Solution 7 - node.jsPratik MundokarView Answer on Stackoverflow
Solution 8 - node.jsAnurag KushView Answer on Stackoverflow
Solution 9 - node.jsDeshan MadurangaView Answer on Stackoverflow