I am trying to fetch to get local json data file in VScode, but no success, I tried everything! :((
Can anyone help me please?
2
fetch is used for requests to the web server.
fetch
You can import it directly if you are not running a web server:
import * as testContent from './test.json';
You don’t use fetch to read a local JSON file; instead, you use the readFile() method as demonstrated below:
readFile()
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");
Click here to cancel reply.
2
Answers
fetch
is used for requests to the web server.You can import it directly if you are not running a web server:
You don’t use
fetch
to read a local JSON file; instead, you use thereadFile()
method as demonstrated below: