Can require be used in a typescript file in the web app to import a json file locally?
export function getData(id: string): string {
const ids: string = id + 'something';
const data = require(`./config/${ids}.json`);
return JSON.stringify(data);
}
If you want a JSON string from a file at a relative address in the same origin as your web app, you can use this function:
export async function getJsonById(id: string):Promise<string> {
const url = `./output/config/${id}something.json`;
const json = (await fetch(url)).text(); // this is a string (JSON is a string)
return json;
// alternatively: to parse the JSON into a native JS data type
// return JSON.parse(json);
}
// use:
const json = await getJsonById('id');