What better way to bring in the holidays than by taking a look at Node’s File System (fs) module!
At a high level, Node fs lets us read, write, create, update, and remove files from our computer using JavaScript on the back end, similar to how you would use Ruby or Python.
To use File System, I imported the module:
const fs = require(‘fs’)
For this example, let’s say we have a JSON file named data.json with a list of Christmas movies. We want to add a new movie to the file and save it to a new file called updated.json that doesn’t exist yet.
Our data.json file looks like this:
{
"movies": [
{
"name": "Christmas Vacation",
"director": "Jeremiah Chechik",
"year": "1989"
},
{
"name": "Die Hard",
"director": "John McTiernan",
"year": "1988"
}
]
}
And the object we want to add to the JSON looks like this:
let christmasStory = {
"title": "A Christmas Story",
"director": "Bob Clark",
"year": "1983"
}
First, I read the contents of the data.json file and saved it into a variable:
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) throw err
let obj = JSON.parse(data)
})
I used fs to access the File System module. The first argument readFile() takes is the file we want to read followed by ‘utf8’ which we add otherwise we’ll get a bunch of strange numbers that aren’t converted into characters. Since readFile() is asynchronous, I added a callback function that fires once it’s finished grabbing the data. I added in some minimal error checking, and tapped into data which holds our JSON file data. Finally, since the data read from the file comes to us a string, I converted it to JSON using JSON.parse().
Next, I pushed the new movie, A Christmas Story, into the movies array in the JSON.
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) throw err
let obj = JSON.parse(data)
obj.movies.push(christmasStory)
})
When writing JSON to a file, we have to turn it into a big string, the way it came to us, otherwise the file doesn’t know what to do with the data. To do this, I used JSON.stringify() and saved it into a variable named updated:
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) throw err
let obj = JSON.parse(data)
obj.movies.push(christmasStory)
let updated = JSON.stringify(obj, null, 2)
})
To finish, I used fs.writeFile() which takes the file to write to and then the data you want to write. WriteFile() will create the file if it doesn’t exist.
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) throw err
let obj = JSON.parse(data)
obj.movies.push(christmasStory)
let updated = JSON.stringify(obj, null, 2)
fs.writeFile('updated.json', updated)
})
The finished code looks like this. To run the code, go to the root of your folder and run node yourFileName. Once run, you'll see the new updated.json file appear in the project's root:
const fs = require('fs')
let christmasStory = {
"title": "A Christmas Story",
"director": "Bob Clark",
"year": "1983"
}
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) throw err;
let obj = JSON.parse(data)
obj.movies.push(christmasStory)
let updated = JSON.stringify(obj, null, 2)
fs.writeFile('updated.json', updated)
})
We only scratched the surface of fs, but as you can see, the module gives us endless possibilities for creating and adding data files in our applications.
Top comments (3)
I got your reference.
I mean, why not just
require
the json file? But good example!Sure, or I could just use
obj.array.push()
but that would kinda defeat the purpose of writing a post about creating files with fs, ha. Thanks for reading!