How to Write to a file in JavaScript

Writing a file in Javascript is as easy as using the built-in method fs.writeFile, and as such matches the read file method quite well

We require the fs package, and use it’s writeFile function to write the contents to the file.js file.

This method is async - there is a sync version as well.