To write in files such as text, csv, JSON,html, etc using node js, The easiest way to do it in Node.js is to use the fs.writeFile()
and writeFileSync()
modes.
Node JS allows you to write files like Text, CSV, JSON, HTML, etc, synchronously and asynchronously. To write files in synchronous mode, you can use fs.writeFileSync()
and write files in asynchronous mode, you can use fs.writeFile()
.
Here are two ways to write files like text, csv, JSON, HTML, etc, in Node JS:
1. fs.writeFile() function
The writeFile()
function writes the file’s data asynchronous or async. it means the writeFile()
method does not block the execution of the next program until the first program is completed.
Here is the basic syntax:
fs.writeFile(file[, options], callback)
The fs.writeFile()
function takes three arguments; is as follows:
- To write the path of the file.
- An object specifying options for a write operation. This may include properties such as encoding, mode, and flags.
- When the file is written a callback function to be executed.
Here are some examples of asynchronous writes to files in Node.js:
How to Write Text File in Node.js
To write async or asynchronous data to a txt or text file using Node JS, you can do something like this:
const fs = require('fs'); const data = 'Welcome Guys, Add this text content! in .txt file'; fs.writeFile('path-to-file-folder/myTextFile.txt', data, (err) => { if (err) { console.error(err); return; } console.log('Data has been successfully written to myTextFile.'); });
How to Write JSON File in Node.js
To write JSON async or asynchronous in Node JS, you can do something like this:
const fs = require('fs'); const data = { name: 'Tutsmake website', category: 'Software and Development' }; const json = JSON.stringify(data); fs.writeFile('path-to-folder/myFile.json', json, (err) => { if (err) { console.log(err); } else { console.log('Json Data has been successfully written to myFile.json.'); } });
2. fs.writeFileSync() function
The writeFileSync()
function writes the file’s data Synchronous or sync, It means writefileSync()
method blocks the execution of the next program until the first program is completed.
Here is the basic syntax:
fs.writeFileSync(file[, options])
The fs.writeFileSync()
function takes two arguments; is as follows:
- To write the path of the file.
- An object specifying options for a write operation. This may include properties such as encoding, mode, and flags.
Here are some examples of synchronous writes to files in Node.js:
How to Write Sync File in Node JS
To write synchronous data to a txt or text file using Node JS, you can use the fs.writeFileSync() function something like this:
const fs = require('fs'); // Write data to the file 'mynewfile.txt' fs.writeFileSync('myTxtfile.txt', 'Hello world!');
How to Write Sync File in Node JS
To write synchronous data to JSON file using Node JS, you can use the fs.writeFileSync() function something like this:
const fs = require('fs'); const data = { name: 'Tutsmake website', category: 'Software Development' }; // Convert the data object to a JSON string. const jsonData = JSON.stringify(data); // Write the JSON string to a file. fs.writeFileSync('myDataFile.json', jsonData); console.log('MyDataFile JSON file was written successfully.');
Once you have written the data to the files, you can verify by reading the file, simply read the file in Node JS using fs.readFile()
:
// Read the JSON file. const jsonData = fs.readFile('data.json'); // Convert the JSON string to an object. const data = JSON.parse(jsonData); console.log(data);
Note:- Asynchronous mode is to write data in any file like txt, json, html in very less time as compared to synchronous mode.
Conclusion
That’s it; You have learned how to use fs.writeFileSync()
and fs.writeFile
functions to write synchronous and asynchronous files like txt, JSON, and html in node js.