DEV Community

Cover image for CRUD On Node FS Module; A Journey Into The World Of File Systems.
Ademola Babatunde
Ademola Babatunde

Posted on

CRUD On Node FS Module; A Journey Into The World Of File Systems.

File System in Nodejs

The fs module in nodejs allows you to read, delete, update, and create files on your machine, it gives you access to do anything you want with the files. There are several methods within the module that allows you to do this. Allow me walk you through the process.

The cover image was sourced from here

Must-Haves

Obviously, you must have nodejs installed on your machine. After which you should import the fs module.

const fs = require('fs');
Enter fullscreen mode Exit fullscreen mode

All of the operations of the fs module can be done either synchronously or asynchronously.

The synchronous form blocks the Node.js event loop and further JavaScript execution until the operation is complete. Exceptions are thrown immediately and can be handled using try…catch, or can be allowed to bubble up.
source - nodeJs doc

Creating or Writing Files with the module

fs.writeFile() and fs.writeFileSync() are the asynchronous and synchronous respectively ways of writing to a file if it already exists or creating a new file and writing to it if it does not exist. Examples of both methods are below:
The asynchronous way,

fs.writeFile('newfile.txt', "Hello world", (err) => {
    if (err) return callback(err);
    console.log("file created successfully!")
});

Enter fullscreen mode Exit fullscreen mode

and the synchronous way,

 const file = fs.writeFileSync('newfile.txt', "Hello world");
Enter fullscreen mode Exit fullscreen mode

Reading Files

If we want to read the content of the new file we created above, this can be done using the fs.readFile() and fs.readFileSync() methods. There are various encoding formats to read the file in, the default if not is specified is Buffer. Both functions take the path to the file as the first argument, see the example below:

The asynchronous way,

fs.readFile('newfile.txt', (err, data) => {
    if(err) throw err;
});
Enter fullscreen mode Exit fullscreen mode

The content of the file will be inside the data parameter

The synchronous way,

const data = fs.readFileSync('newfile.txt', "utf8");
Enter fullscreen mode Exit fullscreen mode

Renaming a File

Using the fs.rename()or fs.renameSync() method allows us to rename a file to any new name we want. The method takes in two arguments, the path to the file to be renamed and the second argument is the new name of the file.
See examples below:

The asynchronous way,

fs.rename('newfile.txt',  'renamedfile.txt', (err) => {
    if(err) throw err;
});
Enter fullscreen mode Exit fullscreen mode

The content of the file will be inside the data parameter

The synchronous way,

fs.renameSync('newfile.txt',  'renamedfile.txt');
Enter fullscreen mode Exit fullscreen mode

Deleting a File

To delete a file, we have the fs.unlink() and fs.unlinkSync(). These methods take a single argument, the path to the file to be deleted or removed.

The synchronous way,

fs.unlinkSync('renamedfile.txt');
Enter fullscreen mode Exit fullscreen mode

The asynchronous way,

fs.unlinkSync('renamedfile.txt', (err) =>  {
    if(err) throw err;
};
Enter fullscreen mode Exit fullscreen mode

Conclusion

These are just the basic operations that can be done with the fs module on files, I only talked about files. Operations can be done on directories too and many more. For other features of the fs module, visit the nodejs fs documentation

Top comments (0)