For a non-blocking solution, use fs.readFile(), which allows Node.js to handle other operations while the JSON file is being read:
const fs = require('fs');
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
const jsonData = JSON.parse(data);
console.log(jsonData);
});
This is the recommended approach for performance if you’re running a server since it prevents blocking the event loop.