Codementor Events

Working with Nodejs file system (fs) and path module

Published Jan 28, 2022
Working with Nodejs file system (fs) and path module

This is the first part in a probably very long series of posts.
In this post, I'll be explaining how to use the node.js file system (fs) and path module.

Expectations

This post assumes a basic knowledge of JavaScript and Node.js

What is Node.js fs module?

According to the official node.js documentation

The fs module provides a lot of very useful functionality to access and interact with the file system.

In other words, the fs module lets you create, edit and delete files and directories.

Note
JavaScript is synchronous by default and is single threaded. This means that code cannot create new threads and run in parallel.

Enough with the boring stuffs, let's code.
We will be diving into various method available to from the fs module that let's us manipulate files and directories.

To get started, let's create a new node.js project with the following directories

Files structure

Our code will go into the index.js file as you might have guessed.

How to create a new directory

To create a new directory we need to first require the fs module and use a mkdir or mkdirSync method of the fs module. Add this to your index.js

const fs = require("fs");

// create a new directory 'assets' in the root directory
const folderPath = "./assets";

fs.mkdirSync(folderPath);

I know you might be wondering why I used mkdirSync and not mkdir method.
Node.js provides us with a way to work with the file system asynchronously, therefore most fs methods have both the synchronous and asynchronous version. In our case I chose to use the synchronous method.

How to create a file in a directory

Next we will create a text file inside the assets directory using the writeFile or writeFileSync method

let fileContent = "Now is the winter of our discontent
Made glorious summer by this sun of York;
And all the clouds that lour'd upon our house
In the deep bosom of the ocean buried";

// create a file named 'shakespear.txt'
let filePath = folderPath + '/shakespear.txt';
fs.writeFileSync(filepath, fileContent);

That's it.
Now that you've create files and directories, next we will be reading the files in a directory and log them in the console

How to Read all the files in a directory

To get all the files in the assets directory, we will be using the readdir or readdirSync method of the fs module.
Take note, readdirSync returns a array.

// Read and returns the name of all files in the directory
try{
  files = fs.readdirSync(folderPath);
}catch(error){
  console.log(error);
}

Okay, cool. Now we can create directories, create and write to files and list all files in a directory.
Below is the complete code in our index.js.

const fs = require("fs");

// create a new directory 'assets' in the root directory
const folderPath = "./assets";

fs.mkdirSync(folderPath);

// create a file named 'shakespear.txt'
let fileContent = "Now is the winter of our discontent
Made glorious summer by this sun of York;
And all the clouds that lour'd upon our house
In the deep bosom of the ocean buried";

let filePath = folderPath + '/shakespear.txt';
fs.writeFileSync(filepath, fileContent);

// Read and returns the name of all files in the directory
try{
  files = fs.readdirSync(folderPath);
}catch(error){
  console.log(error);
}

To learn more about fs module, visit the official node.js documentation.
In my next posts, I'll be giving more examples of the fs module method such as the rename, where we will bulk rename all the files in our assets folder with a few lines of code and I'll explain more on the path module.

If you have suggestions or corrections, don't hesitate to get in touch.

Discover and read more posts from Majid Kareem
get started
post commentsBe the first to share your opinion
Show more replies