Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
});
like image 953
Thirupparan Avatar asked Jul 03 '18 09:07

Thirupparan


2 Answers

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);
   });
 });
like image 155
Wejd DAGHFOUS Avatar answered Oct 31 '22 14:10

Wejd DAGHFOUS


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.

like image 18
phuzi Avatar answered Oct 31 '22 16:10

phuzi