Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
use require in the ts file to import json file dynamically

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);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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:

TS Playground

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');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!