Loading…

How we can write a file in Node JS?

Syntax of write a file is given below :-

fs.writeFile(filename, data[, options], callback)

Example

                                        
var fs = require("fs"); console.log("Write in existing file"); fs.writeFile('file.txt', 'This is test msg', function(err) { if (err) { return console.error(err); } console.log("Written successfully!"); console.log("Read latest written data"); fs.readFile('file.txt', function (err, data) { if (err) { return console.error(err); } console.log("Read: " + data.toString()); }); });

Suggest An Answer

No suggestions avaliable!

Latest post from Node js Interview Questions

Ask Question