You are currently viewing Mastering File Manipulation in Node.js: A Comprehensive Guide

Mastering File Manipulation in Node.js: A Comprehensive Guide

JavaScript is a popular programming language among web developers. But when it was first released, only front end developers enjoyed all of the fun it had to offer, since you couldn’t run JavaScript outside the browser.

However, in 2009, a groundbreaking technology called Node.js was introduced. Node.js is a JavaScript runtime that enables developers to execute JavaScript code outside of the browser, opening up new possibilities for backend development. It quickly gained popularity among programmers and is now ranked as the 6th most widely used technology.

As a backend developer, it is crucial to have a strong understanding of how to efficiently handle and manipulate files in Node.js. In this guide, we will delve into the intricacies of file management and processing in Node.js, equipping you with the necessary knowledge and skills to excel in your backend development endeavors.

Before diving into the coding aspect, let’s cover a few prerequisites. Do you need an extensive knowledge of Node.js to follow this guide? Not necessarily. However, having a solid understanding of JavaScript will greatly enhance your experience.

Additionally, ensure that you have Node.js installed on your computer if you haven’t done so already. Installing Node.js is a simple process – just visit the official website, download the appropriate version for your operating system, and follow the installation prompts. Once installed, you’ll be ready to proceed with the rest of this guide effortlessly.

Exploring the Node.js File System: Managing Files with Ease

Node acts as a file server. With the Node module you can work with files on your computers and carry out various operations. When you’re building a web app, you might want to upload images, a résumé, videos, or docs to the server. Well, the Node file system has the ability to do all of that and more.

Since this is a beginner-friendly guide, I’ll explain what Node modules are in general first before we talk more in-depth about the file system.

What are Node modules?

Incase you’re not familiar with the term, Node modules are simply reusable JavaScript code. You can easily import modules along the line throughout your project. The purpose of modules is to ensure that you’re not constantly repeating the same code throughout a project.

There are multiple types of Node modules – local modules, built-in modules, and third-party modules. In this guide we’ll focus on the built-in modules.

Here’s a list of Node’s built-in modules:

  • fs
  • http
  • url
  • os
  • path

The fs is the file system module in Node. It’s all making sense now, right? There’s a whole library of node modules.

How to Work with the Node fs Module

The fs module allows you to carry out several operations that involve files and folders. With the fs module you can:

  • read files
  • create files
  • update files
  • delete files
  • rename files

To get started with this world of file handling, first we’ll create a folder. Then we’ll create a JavaScript file for now. Open the JS file with VS Code or any text editor you are happy with, and let’s write some code.

How to Read Files

To read the files directly from your computer, you’ll have to use the use the fs.readFile() method. For this to work you’ll need to create the file you want to read. Let’s assume that it’s a txt file with the content hello world. Let’s call it dummyText.txt.

Now there are multiple ways you can read a file with Node – either using the http module, which will read your file on the localhost, or using promises. I’m going to show you both, and which one is preferable.

Let’s start with using the http module. Open your JS file, let’s call it readfile.js, and add the following code:

const http = require('http'); 
const fs = require('fs');

http.createServer(function (req, res) { 
   fs.readFile('dummyText.txt', function(err, data) { 
      res.writeHead(200, {'Content-type': 'text/html'}); 
      res.write(data);           
      return res.end(); 
    }); 
 }).listen(8080);

Save the code, and then open cmd in the file directory and run Node readfile.js:

‌ C:\Users\nodefilesystem>node readfile.js

Visit https://localhost:8080 to see your results. You should see the content of the dummy text on your browser.

Now let’s recreate this with promises. The promise way is a more modern programming style, as we’ll incorporate the new async and await syntaxLet’s read our file using promises.            

// import promises 
const { readFile } = require('fs/promises'); 

async function readThisFile('./dummytext.txt') { 
  try { 
    const data = await readFile('/dummytext.txt');
    console.log(data.toString()); 
  } catch (error) { 
    console.error(`Got an error trying to read the file: {error.message}`); 
  } 
 }

Let’s explain the code. So we imported the Node promises module using the file system. We used fs/Promises to processes our files with promises.

We used the readFile method to process our file that we want to read. Using promises we can read our files in an asynchronous manner. Inside the readFile method we have the path to our dummytext that we want to read, then return the content of the file.

We also used a try-catch error handling method to ensure that we can properly control what the error message reads. So, if there’s something wrong with the code before the catch line, It’ll output “Got an error trying to read the file”, instead of an untidy error message.

Error-handling will also prevent the program from stopping when it spots an error. With our code, the program can still resume even if it’s interrupted.

How to Create Files

Now that we’re familiar with the promises method of working with the file system, we’re going to be using that throughout the rest of the guide. Now that we know how to read the contents off an existing file, how about we learn how to create them?

To create a file we can use one of three methods: appendFile()open(), and writeFile().

Let’s open our text editor and start with appendFile():

const { appendFile} = require('fs/promises'); 

async function appendToFile('anotherDummyText.txt', 'i love node') { 

  try { 
    await appendFile(fileName, data, { flag: 'w' });                   console.log(`Appended data to ${fileName}`); 
  } catch (error) { 
    console.error(`Got an error trying to append the file: {error.message}`); 
  } 
 }

Let’s explain the code a little bit more. We already called the module, and now we are using the appendFile method to write to the file we created earlier. If the program doesn’t see any file with the name we stated in the appendToFile function, it’ll create it.

Also we used the try-catch error handling method to make sure to control the errors.

Next, we’ll work with WriteFile. It’s pretty much the same code, but with a few adjustments:

const { writeFile } = require('fs/promises'); 

async function writeToFile('dummyText.txt', 'using write method') { 
   try { 
     await writeFile(fileName, data); 
     console.log(`Wrote data to ${fileName}`); 
   } catch (error) { 
     console.error(`Got an error trying to write the file: ${error.message}`); 
   } 
 }

Now we are using the writeFile method which creates the file directly and adds the data to it. Also take note of the await keyword, which we need to run a successful JavaScript promise.

How to Rename Files

Another major part of working with files is renaming an already existing file. Let’s take a look at how that works:

const { rename } = require('fs/promises'); 

async function renameFile('dummytext.txt', 'changedDummyText.txt') { 
   try { 
     await rename(from, to); 
     console.log(`Renamed ${from} to ${to}`); 
   } catch (error) { 
     console.error(`Got an error trying to rename the file: ${error.message}`); 
   } 
 }

As you can see, our async function took in two parameters this time, the first one will old the current name of the document we are about to change, and the second one will old the new name we are working with.

We’ve also changed our dummyText.txt to changedDummyText.txt.

How to Delete Files

To delete a file we’ll have to call the unlink method. This time, instead of having two parameters, we’ll have only one: the name, or the path of the file you want to delete.

We’ll also still use our try-catch error handling method to control our errors, and log the name of the file that has been deleted on the console.

const { unlink } = require('fs/promises'); 

async function deleteFile('./dummytext.txt') { 

   try { 
     await unlink(filePath); 
     console.log(`Deleted ${filePath}`); 
   } catch (error) { 
     console.error(`Got an error trying to delete the file: ${error.message}`); 
   } 
 }

How to Update Files

Yes, I did mention that we will learn how to update files too – but if you’ve been paying close attention, you’ll see that we already did.

We talked about appendFile(), which basically changes key information, and allows you to write new contents of the file. We also talked about writeFile(), which updates the contents of a file as well.

Conclusion

Here we are, the end of this guide. Let’s recap what we’ve learned in this tutorial.

First, we talked about the what Node.js is, and how important the file system is. We also learned what Node modules are. All of this is vital to you understanding the rest of the tutorial.

Keep in mind there are more operations that you can perform with the file system in Node. This tutorial just covered the basics, and the primary functions. Read up a bit on it and you’ll discover a lot more.

If you loved this article, let’s talk on Twitter. I’d love to connect with each and every one of you.

Leave a Reply