Skip to main content

Read a json file in Node.js

To read any file in Node.js, you need to import the fs or file system module. From there, you have two methods from which to choose: readFile, and readFileSync.

Both are similar in that they return a Buffer of binary data from a file. readFile accepts a callback that will receive the Buffer, while readFileSync returns it synchronously.

In action, they look like:

const fs = require("fs");

fs.readFile("./data.json", (error, data) =>
  error ? console.error(error) : console.log(data)
);

try {
  console.log(fs.readFileSync("./data.json"));
} catch (error) {
  console.error(error);
}

If you run this code in a directory with a data.json file, boths methods will log something like:

<Buffer 7b 0a 20 20 22 64 61 74 61 22 3a 20 74 72 75 65 0a 7d>

Both are returning the same data, but it isn’t consumable. In most instances, you will need to do one more operation in order to use the data that you have read from a file.

Since we are working with a json file, we can use the global JSON object to read the data. The parse method will convert this Buffer into a usuable JSON object:

const fs = require('fs')

fs.readFile('./data.json', (error, data) => (
    error ? console.error(error) : console.log(JSON.parse(data))
))

try {
    console.log(JSON.parse(fs.readFileSync('./data.json')))
} catch (error) {
    console.error(error)
}

Run it again, and you will see whatever data you had stored in your json file logged to the console. You have successfully read a json file in Node.js!

You can see the code in action here:

Happy coding!