What is the best way to use JavaScript to read a JSON file from the local file system, and how can I print its contents?

I’ve worked a lot with Node.js over the years, and honestly, the easiest way to handle local files is within that environment. When you’re working on backend scripts or CLI tools and you need to do a javascript read json file task, Node.js makes it super straightforward using the built-in fs module.

const fs = require('fs');

fs.readFile('/Users/Documents/workspace/test.json', 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading file:', err);
    return;
  }
  const jsonData = JSON.parse(data);
  console.log(jsonData);
});

Just make sure your script has access to that file path and you’re good to go.