66

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);
});

10 Answers 10

89

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);
   });
 });
2
  • 4
    Perfect! But, why are we defining a function again for writeFile as the 3rd parameter, when have the 'data' param from readFile that holds the data? Why can't we reuse the same data? Feb 6, 2019 at 1:30
  • 1
    There is no need to pass the third parameter in the callback function of writeFile. The only thing is that we should pass a callback function.
    – Kumar Ajay
    May 9, 2020 at 3:54
19

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.

0
12

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

Node.js documented the purpose for the change: https://github.com/nodejs/node/blob/master/doc/api/deprecations.md#dep0013-fs-asynchronous-function-without-callback

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

6

you also use like this

var file2 =  fs.readFileSync("./Public/n2.jpeg")
3
  • 1
    There's no need to use await when you use readFileSync because, as the name suggests, readFileSync is synchronous. Jun 16, 2020 at 5:24
  • await and readFileSync is completely redundant since readFileSync its an already sync function Jul 2, 2020 at 8:47
  • When I do this I get Callback must be a function
    – Yeats
    Oct 20, 2021 at 23:25
1

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

1

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);`});`
0
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);

    });
});
1
  • While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation
    – ysf
    Jun 6, 2020 at 22:01
0

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) => {
    readFile(path,'utf8',(err,result)=>{
      if(err)
        reject(err);
      else
        resolve(result);
      })
  })
}

readText('./contents/sample.txt')
  .then(val=>writeFileSync('./results.txt',val))
  .catch(err=>console.log(err));
0

I went into a similar situation. I followed below.

const fs = require('fs').promises;

Now you can read/write the file with await without using any kind of callback. Here we are Promising the fs module.

const file = await fs.readFile(fileURL);

-1

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

var hello = myfunction( callme() );

rather than

var hello = myfunction( callme );

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.