Node js file system, Easier than we think(1).
Tags: node, javascript, react, programming
Published: 2023-06-11 14:52:57

**What is Node js?**
If we want to run javascript without a browser, we must use an environment to execute js code. Using Node js we can create that runtime environment to execute js code.

It's built upon Chrome's v8 engine using C++.

We need to install Node js [Node js](https://nodejs.org/en/download) in our system. We also need the editor to write codes. I'm going to use [Visual studio](https://code.visualstudio.com/download) to write and execute javascript code.

**What is File system?**
The file system module allows us to work with files in our system. Using the file system we can perform various tasks just like,

- Create file

- Read file

- Update file

- Delete file

- Rename file

**Need to know**
The basic idea is to run a node js file.

**Create File**
There are three different methods to create new files,

1. fs.writeFile()
2. fs.open()
3. fs.appendFile()

> here 'fs' represent `var fs =require('fs')`

**fs.writeFile()**
`fs.writeFile` method helps us to create and write something on a file if that file does not exist. If that file already exists then it will replace with new content. 

```
fs.writeFile( file, data, options, callback )
```
In the above line, 

- file: represent, string, buffer, url or file path
- data: represent specific content.
- options: encoding, mode, flag
- callback: we can set a function that will be called after that (fs.writeFile) function executed.

**Example**
write the below code to create a file with its content. Now execute `node index.js`(file name) command.

```
var fs = require('fs');

//create a file named firstFile.txt:
fs.writeFile('firstFile.txt', 'Hello world!', function (err) {
    if (err) throw err;
    console.log('Saved!');
});
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzvks53obaxu2qaasrpn.png)

above code will create a text file named 'firstFile.txt' and its content will be 'Hello world!'


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qd0retbi0d8xwvrh94nl.png)

if you change its content 'Hello world!' to something else, then the will be replaced with the new content. 

_Example with encoding, mode and flag,_

```
fs.writeFile('firstFile.txt', 'Hello world!',
  {
    encoding: "utf8",
    flag: "w",
    mode: 0o666
  },
  function (err) {
    if (err) throw err;
    console.log('Saved!');
});
```

**fs.open()**
This method works differently than others, it takes the second parameter as a "flag", and if the second parameter is "w" for "writing", then the file is opened for writing. if the file does not exist then it will create an empty file.

```
var fs = require('fs');

fs.open('firstFile.txt', 'w', function (err, file) {
  if (err) throw err;
  console.log('Saved!');
});
```
execute node index.js(filename) to see the changes.

types of flags are described below,

**- w:** Open file for writing. A file is created if it doesn’t exist.
**- w+:** Open the file to read and write. A file is created if it doesn’t exist.
**- wx:** It is the same as ‘w’ but fails if the path exists.
**- wx+:** Open the file to read and write. A file is created if it doesn’t exist.
**- r:** To open the file to read and throws an exception if the file doesn’t exist.
**- r+:** Open the file to read and write. Throws an exception if the file doesn’t exist.
**- rs+:** Open files in synchronous mode to read and write.
**- a:** Open the file to append. A file is created if it doesn’t exist.
**- ax:** It is the same as a but fails if the path exists.
**- a+:** Open the file for reading and appending. A file is created if it doesn’t exist.
**- ax+:** It is the same as ‘a+’ but fails if the path exists.


**fs.appendFile()**
It is used to asynchronously append the given data to a file. If the file does not exist, the file will be created

```
fs.appendFile( path, data[, options], callback )
```
In the above line,

- path: It can be string, buffer, url

- data: It can be string, buffer.

- options: encoding, mode, flag

- callback: we can set a function that will be called after that (fs.appendFile) function executed.

below code helps us to understand more clearly, here, 'utf8' is refers to "encoding"

```
var fs = require('fs');

var data = "appendFileTest";

// Append data to file
fs.appendFile('appendFile.txt', data, 'utf8',
    // Callback function
    function () {
        console.log("Nicely done.")
    });
```
Now, run the code with 
`node index.js // index.js is file name.`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1zgzbjwtm723zfw7elm.png)
below image shows we created a file and its name "appendFile.txt"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3h7vf1p2sj5lk0nnqdr.png)

that's for today, see yaa.