is there a require for json in node.js is there a require for json in node.js json json

is there a require for json in node.js

As of node v0.5.x yes you can require your JSON just as you would require a js file.

var someObject = require('./somefile.json')

In ES6:

import someObject from ('./somefile.json')

JSON files don’t require an explicit exports statement. You don't need to export to use it as Javascript files.

So, you can use just require for valid JSON document.


{  "name": "Freddie Mercury"}


var obj = require('data.json');console.log(; //Freddie Mercury

Two of the most common

First way :

let jsonData = require('./JsonFile.json')

let jsonData = require('./JsonFile') // if we omitting .json also works


import jsonData from ('./JsonFile.json')

Second way :

1) synchronously

const fs = require('fs')let jsonData = JSON.parse(fs.readFileSync('JsonFile.json', 'utf-8'))

2) asynchronously

const fs = require('fs')let jsonData = {}fs.readFile('JsonFile.json', 'utf-8', (err, data) => {  if (err) throw err  jsonData = JSON.parse(data)})

Note: 1) if we JsonFile.json is changed, we not get the new data, even if we re run require('./JsonFile.json')

2) The fs.readFile or fs.readFileSync will always re read the file, and get changes