Node.js supports using require('/path/to/file-name.json') to import a JSON file. It is equivalent to this code:
const fs = require('fs');
module.exports = JSON.parse(
fs.readFileSync('/path/to/file-name.json', 'utf8'));
That is, the JSON file is read synchronously, and the text is parsed as JSON. The resultant object is available as the object exported from the module. Create a file named data.json, containing the following:
{
"hello": "Hello, world!",
"meaning": 42
}
Now create a file named showdata.js, containing the following:
const util = require('util');
const data = require('./data');
console.log(util.inspect(data));
It will execute as follows:
$ node showdata.js
{ hello: 'Hello, world!', meaning: 42 }
The util.inspect function is a useful way to present an object in an easy-to-read fashion.