Coding Exercise: Write to a File

Learn to write data to a file.

We'll cover the following

Write to a file

Let’s implement a writeFile() function that asynchronously writes data to test.txt, replacing all existing data with new data passed to writeFile().

We can write to a file with fs.writeFile(). This method accepts two arguments:

  • It accepts a file path.
  • It accepts data to be written to the file.

After writing to test.txt with the implemented function, check the contents of test.txt displayed by the readFile() function.

Let’s add the required code to the file-system.js file and see what happens:

Get hands-on with 1200+ tech skills courses.