Home >> Tutorials >> Node.js File System: Write to File Node.js
Node.js File System: Write to File Node.js:

The Node.js file system module (fs module) allows you to work with the files.

The fs.writeFile() is used to write to file.

There are two ways to write file

  1. Asynchronously
  2. Synchronously
1. Write file Asynchronously:
var fs = require('fs');
var content = "Node.js fs module's function - fs.writeFile() is used to write to file.";
fs.writeFile('my_file.txt', content , (err) => {
	if (err){
	 throw err;
	}else{
	 console.log('Your Content written to file successfully!');
	}
});
2. Write file Synchronously:
var fs = require('fs');
var content = "Node.js fs module's function - fs.writeFile() is used to write to file.";
fs.writeFileSync('my_file.txt', content);
console.log("Your Content written to file successfully!");