Using fs (File System) Module in Node js to Write and Read File - Node Js Tutorial

July 13, 2019


Hi!, in this article I am going to make a tutorial about how to use fs Module in Node.js to create file and accessing the file.

Using fs (File System) Module in Node js to Write and Read File - Node Js Tutorial

What is fs Module ?

The Node.js file system module allows us in order work with the file system on our local computer.
fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file example, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file example, make json fie from fs,
Using fs (File System) Module in Node js to Write and Read File - Node Js Tutorial


Using fs (File System) Module in Node js to Write file

To use this file system module we just need to include this modue in our node file (app.js/index.js - whatever you name it)
const fs = require('fs')

In here I am trying to use fs module in node js to store our data to a file that is created by fs module in node js.

Here is the example of the use of fs module in node js. In this example we just want to make a file and store some content inside the data

const fs = require('fs') //we load the fs module

const data = { // we make a constant variable that consist of data object
    address: 'Wroclaw',
    country: 'Poland'
}
//here we use fs module that use writefilesync function
//that allow us to make a file
fs.writeFileSync('data.txt', data) 

Now you can see that data.txt file has already created and some contents are written there.


Using fs (File System) Module in Node js to Read file

Okay, so what if we want to read the data ?
//now we want to read the data from data.txt
const readBuffer = fs.readFileSync('data.txt')
//because the data from readbuffer is retrieved as binary,
//we need to make it string
const toStringBuffer = readBuffer.toString()
console.log(toStringBuffer)

Quite easy right ?
So, now we are going to learn how to make a json file from fs.

const fs = require('fs')

const data = {
    country: 'Indonesia',
    address: 'jakarta'
}

const dataToJson = JSON.stringify(data)
fs.writeFileSync('json3.json', dataToJson)

//now we want to read the data from json3.json
const readBuffer = fs.readFileSync('json3.json')
console.log(`it is what readbuffer got : ${readBuffer}`)

//because the data from readbuffer is retrieved as binary,
//we need to make it string
const toStringBuffer = readBuffer.toString()
console.log(`it is after readbuffer is converted to string : ${toStringBuffer}`)

//then parse it, to make it as an object 
//so we can change the value
const newData = JSON.parse(toStringBuffer)

newData.country = 'Poland'
newData.address = 'ul. wittiga, wroclaw'

//it is done! we already change the value,
//but as it's in the object form, we need to make it as JSON form
//using json strinngify function
const newNewdata = JSON.stringify(newData)

//save to the json3.json file
fs.writeFileSync('json3.json', newNewdata)

I hope it can be useful and have a nice day!

tag :
fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file example, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file, fs module javascript, fs module node, fs module npm, fs module js, fs module read file, fs module create file example, make json fie from fs,

Share this

Related Posts

Previous
Next Post »