var fs = require('fs');var stream = fs.createWriteStream("my_file.txt");stream.once('open', function(fd) {stream.write("My first row\n");stream.write("My second row\n");stream.end();});
fs = require('fs');fs.writeFile('helloworld.txt', 'Hello World!', function (err) {if (err)return console.log(err);console.log('Wrote Hello World in file helloworld.txt, just check it');});
/* The fs.createWriteStream() returns an (WritableStream {aka} internal.Writeable) and we want the encoding as 'utf'-8 *//* The WriteableStream has the method write() */fs.createWriteStream('out.txt', 'utf-8').write('hello world');
const fs = require('fs');const data = {table:[{id: 1, name: 'my name'}]}const file_path = './my_data.json'writeFile(file_path, data)async function writeFile(filename, writedata) {try {await fs.promises.writeFile(filename, JSON.stringify(writedata, null, 4), 'utf8');console.log('data is written successfully in the file')}catch (err) {console.log('not able to write data in the file ')}}