CRUD On Node FS Module; A Journey Into The World Of File Systems.
Tags: javascript, webdev, node
Published: 2021-10-05 13:42:12

## 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.

<sub><sup>The cover image was sourced from [here](https://www.digitalnamanji.com/what-is-file-system/)</sup></sub>

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

```js
const fs = require('fs');
```

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](https://nodejs.org/api/fs.html#fs_file_systeml)

## 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,

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

```
and the synchronous way,
```js
 const file = fs.writeFileSync('newfile.txt', "Hello world");
```

## 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,

```js
fs.readFile('newfile.txt', (err, data) => {
    if(err) throw err;
});
```
 
The content of the file will be inside the data parameter


The synchronous way,

```js
const data = fs.readFileSync('newfile.txt', "utf8");
```

## 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,
```js
fs.rename('newfile.txt',  'renamedfile.txt', (err) => {
    if(err) throw err;
});
``` 
The content of the file will be inside the data parameter


The synchronous way,
```js
fs.renameSync('newfile.txt',  'renamedfile.txt');
```

## 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,
```js
fs.unlinkSync('renamedfile.txt');
```
The asynchronous way,
```js
fs.unlinkSync('renamedfile.txt', (err) =>  {
    if(err) throw err;
};
```

## 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](https://nodejs.org/api/fs.html)