skip to Main Content

I am trying to fetch to get local json data file in VScode, but no success, I tried everything! :((

Can anyone help me please?
enter image description here

2

Answers


  1. fetch is used for requests to the web server.

    You can import it directly if you are not running a web server:

    import * as testContent from './test.json';
    
    Login or Signup to reply.
  2. You don’t use fetch to read a local JSON file; instead, you use the readFile() method as demonstrated below:

    const { readFile } = require("fs/promises");
    
    async function readJsonFile(filename) {
      const data = await readFile(filename, "utf8");
      const parsedData = JSON.parse(data);
      console.log(parsedData);
    }
    
    readJsonFile("./test.json");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search