NodeJS: How To Read A JSON File From Your Machine
Tags: javascript, node, eli5
Published: 2019-08-26 12:29:08

---
title: NodeJS: How To Read A JSON File From Your Machine
published: true
description: How To Read A JSON File From Your Machine
tags: javascript, node, eli5
series: Intro to NodeJS
---

## Intro

So we [installed NodeJS](https://dev.to/miku86/what-is-nodejs-how-to-install-it-1900) on our machine.

Now we want to learn how to read a JSON file from our machine using the [File System (FS) module](https://nodejs.org/api/fs.html).

## Create a file with some data

- Open your terminal
- Create a file named `data-read.json`:

```sh
touch data-read.json
```

- Add some JSON data into it:

```json
[{ "id": 1, "name": "miku86" }]
```


## Write a simple script

- Open your terminal
- Create a file named `index.js`:

```sh
touch index.js
```

- Add this JavaScript code into it:

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

const FILE_NAME = 'data-read.json';

const readFileAsync = () => {
  fs.readFile(FILE_NAME, (error, data) => {
    console.log('Async Read: starting...');
    if (error) {
      console.log('Async Read: NOT successful!');
      console.log(error);
    } else {
      try {
        const dataJson = JSON.parse(data);
        console.log('Async Read: successful!');
        console.log(dataJson);
      } catch (error) {
        console.log(error);
      }
    }
  });
};

readFileAsync();
```

Note: We are using the async [`readFile`](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback) function to read data, because we don't want to block other tasks. You can also read data synchronous using [`readFileSync`](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options), but this could block some other tasks.

Note: You can do a lot of stuff with the File System module, therefore [read the docs of the FS module]( https://nodejs.org/api/fs.html#fs_file_system_flags).

---


## Every line explained

```js
// import the file system module
const fs = require('fs');

// save the file name of our data in a variable (increase readability)
const FILE_NAME = 'data-read.json';

const readFileAsync = () => {
  // run async function to read file
  fs.readFile(FILE_NAME, (error, data) => {
    console.log('Async Read: starting...');

    if (error) {
      // if there is an error, print it
      console.log('Async Read: NOT successful!');
      console.log(error);
    } else {
      try {
        // try to parse the JSON data
        const dataJson = JSON.parse(data);
        console.log('Async Read: successful!');
        console.log(dataJson);
      } catch (error) {
        // else print an error (e.g. JSON was invalid)
        console.log(error);
      }
    }
  });
};

// run the function
readFileAsync();

```

---

## Run it from the terminal

- Run it: 

```sh
node index.js
```

- Result: 

```sh
Async Read: starting...
Async Read: successful!
[ { id: 1, name: 'miku86' } ]
```

---

## Further Reading

- [File System module](https://nodejs.org/api/fs.html)
- [File System Flags](https://nodejs.org/api/fs.html#fs_file_system_flags)


---

## Questions

- Did you ever use the [`fs Promises API`](https://nodejs.org/api/fs.html#fs_fs_promises_api), that uses Promises instead of Callbacks?