Complete 2024 Web Development Bootcamp

Dr. Angela Yu

Back to Node.Js Index


Node.Js
File Read / Write
File Write

To use Node.Js to create a new file and write to it, we're going to use the File System module. So we need to "import" the module into our Js file:

  • JavaScript
  • // import the file system module
  • const fs = require("fs");

Now that we have access to the File System module, we can write our file. The basic syntax is:

  • fs.writeFile ( path/fileName, data[, options], callback)
So we're using the writeFile method of the fs module.
  • - path/fileName: the path and file name to be written - entered as a "string"
  • - data: the file data to write - this can be a variable or actual text written as a "string"
  • - options: these are optional parameters that can be use for file processing - we'll use this when we read a file
  • - callback: this is the callback function which could return any error encountered when trying to write the file

So, for example:

  • JavaScript
  • // import the file system module
  • const fs = require("fs");
  •  
  • // create a new file and add some data
  • fs.writeFile ( "message.txt", "Hello from Node.Js", (err) => {
    • if (err) {
      • console.error('Error writing to file:', err);
      • return;
    • }
    • console.log('File written successfully!');
  • });
So we are using the writeFile method of the fs module. We are creating a file called message.txt and we are going to write the text Hello from Node.Js into this newly created file. Incidentally, there is no path associated with the file name, so the file will be created in the CWD (current working directory).

We have added a variable (err) to our callback function. If there is an error creating or writing to this file, then the callback function will return the error, which we can display via the err variable. If there is no error and the file is created and written to successfully, then we just log a message indicating a successful process.


File Read

Now to read from an existing file is essentially the same thing. The basic syntax is:

  • fs.readFile ( path/fileName[, options], callback)
So we're using the readFile method of the fs module.
  • - path/fileName: the path and file name to be read - entered as a "string"
  • - options: these are optional parameters that can be use for file processing - we'll use this in our next example
  • - callback: this is the callback function which could return any error encountered when trying to read the file

So, for example:

  • JavaScript
  • // import the file system module
  • const fs = require("fs");
  •  
  • // read from an existing file
  • fs.readFile ( "message.txt", "utf8", (err, data) => {
    • if (err) {
      • console.error('Error reading from file:', err);
      • return;
    • }
    • console.log(data);;
  • });
Once again, we're using the readFile method of the fs module. We are "reading" from a file in the CWD (current working directory), called message.txt.

Notice here that we are using an optional argument to specify that we want the output to be in utf8 format. This is so that we get actual text and not unencoded buffer data. We could have omitted this and converted our buffer data to a string in the output, like so:

  • console.log(data.toString());
And,once again, if there is any error reading the file, we log the error. If not, we log the file contents.


Back to Top