Eric The Coder
Eric The Coder Blog

Eric The Coder Blog

Create a backend in Javascript (part 3): NodeJS Files and Folders Manipulation

Create a backend in Javascript (part 3): NodeJS Files and Folders Manipulation

Eric The Coder's photo
Eric The Coder
·Oct 4, 2021·

5 min read

Here is a series of articles to help you create backend applications in Javascript.

Node.js is now a must, so it is essential for a developer to master it.

I will publish a new article every two days and little by little you will learn everything there is to know about Node.js

To not miss anything follow me on twitter:

Modules included with NodeJS

NodeJS comes with several modules with functions that allow you to easily perform the most common tasks.

You will see later that it is also possible to add other modules created by the community and extend the functionality of your application almost unlimited.

Here I will present you only a few examples of NodeJS modules. You can consult the NodeJS documentation for full details of the modules and how they work.

The Path module

The 'path' module contains functions that allow proper handling of folder and file locations

To reference a module, just use its name

const path = require('path')

products_path = '/data/products/products.json'
products_file = path.basename(products_path)

console.log(products_file) // products.json

The path.basename() function allows you to return the name of the file only so in this case 'products.json'

Another handy function is path.join(). This function allows you to join together one or more folders and files. example :

const path = require('path')

data_folder = '/data/'
products_folder  = '/products'
products_file = 'products.json'

const full_path = path.join(data_folder, products_folder, products_file)

console.log(full_path) // /data/products/products.json

path.join() concatenates all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path

Finally, sometimes you would like to have the absolute path on the server

const path = require('path')

data_folder = '/data/'
products_folder  = '/products'
products_file = 'products.json'

const full_path = path.join(data_folder, products_folder, products_file)

const abs_path = path.resolve(__dirname, 'data', 'products', 'products.json')

// /Users/username/Documents/dev/learn_node/data/products/products.json

path.resolve() process the sequence of paths from right to left, with each subsequent path prepended until an absolute path is constructed.

The File System module

Undoubtedly one of the most used module, this module allows you to handle files and folders on the server

The FS module allows the manipulation of files and folders in two different ways. You can do this in synchronous or asynchronous mode.

Synchronous functions

This means that this function is blocking, NodeJS will wait for the function return value before resuming the execution of the application.

Asynchronous functions

This means that NodeJS will not wait for the function return value, it will continue your application execution and when the function is finished, will process the result.

Which method to use?

It depends on the type of application you want to develop. If for example you are developing a web server in this case it is preferable, not to say essential, to use an asynchronous function. A synchronous function would block the execution of the server not only for the current user but also would block all users.

On the other hand in certain very precise cases the use of a synchronous function can be justified. For example, before launching a web server, if you need to read a configuration file, in this case, a synchronous function guarantees that you will read the file before the server is launched.

In short, in general, always used an asynchronous function and only if necessary, a synchronous function.

Let's see an example of using the 'fs' module with the two types of functions:

Synchronous function

// app.js

const fs = require('fs')

const data = fs.readFileSync('info.txt', 'utf-8')
console.log(data) // file content
console.log('The file has been read')

Here the result is easily predictable, the code will be executed line by line.

Asynchronous function

const fs = require('fs')

const info = fs.readFile('info.txt', 'utf-8', (err, data) => {
console.log('The file has been read')

Here, NodeJS will not wait for the function to return to continue execution.

This will have the consequence of displaying 'The file has been read' first and when the readFile() has finished its work, NodeJS will execute the callback function console.log(data)

Reading and creating a file

Note that for the rest of this tutorial we will only use asynchronous functions.

First we will create a text file. To do this we will use the writeFile function

const fs = require('fs')

const data = 'This is my Hello World file'

fs.writeFile('info.txt', data, 'utf-8', (err) => {
    console.log('File created')

The writeFile () function is quite self-descriptive. You must specify the file name, data and encoding option. (utf-8 for text)

If you run this code, the info.txt file will be created in the current folder.

Then it will be possible to read this file with the readFile function

const fs = require('fs')

const info = fs.readFile('info.txt', 'utf-8', (err, data) => {

If the file does not exist you can return an error

const info = fs.readFile('info.txt', 'utf-8', (err, data) => {
    if (err) 

It is also possible to read a file with readFile but using promises.

const fs = require('fs').promises

const start = async () => {
    const data = await fs.readFile('info.txt', 'utf8')


Different import and different syntax but same result

Copy a file

To copy a file we use the copyFile function

fs.copyFile('info.txt', 'info2.txt', (err) => {
      if (err) return console.error(err)
        console.log('File copied')

Create a folder

To create a folder we use the mkdir function

fs.mkdir('data', (err) => {
    console.log('Data folder created')

The folder is created inside the current folder

List files in a folder

It is possible to get the list of files in a folder

fs.readdir('.', (err, files) => {

'.' represents the current file files is a array containing all folder file name

Rename a file

To rename a file we use the rename() function

fs.rename('info.txt', 'data.txt', (err) => {
    if (err) return console.log(err)
    console.log('File renamed')

The possibilities are almost endless!

You now have a base with the 'fs' module if you want to know all the available 'fs' functions, consult the NodeJS website for all the details:


That's all for today, follow me on twitter: to be notified of the publication of the next article (within two days).

Did you find this article valuable?

Support Eric The Coder by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this