The encoding option is ignored if data is a buffer. It defaults encoding is ‘utf8’, Default file mode is 0666 and default flag is used ‘w’ means write mode.


The fs.writeFile() function in Nodejs (JavaScript) uses the following syntax. Here:


Let’s create a JavaScript file testWriteFile.js and add the following content. This script will write the “Hello World!” string into the file named output.txt in the current directory.

Save file and run it with Node.js You can also view the content of output.txt with type command on Windows or cat command in Linux.