0

How can I refactor this function, so that it works with vite:

export function readFileFromDisk(file, folder) {
  // console.log(`Reading file ${file} from ${folder}`);
  let book = "";
  if (typeof folder !== "undefined") {
    book = JSON.parse(JSON.stringify(require(`../data/${folder}/${file}`)));
  } else {
    book = JSON.parse(JSON.stringify(require(`../data/${file}`)));
  }
  return book;
}

Currently vite reports Uncaught ReferenceError: require is not defined.

I cannot use import as it must precede all other statements, which is not possible with this current function syntax.

jimmybondy
  • 2,092
  • 3
  • 21
  • 33
  • Does this answer your question? [how to import a json file using vite dynamicly](https://stackoverflow.com/questions/67822238/how-to-import-a-json-file-using-vite-dynamicly) – Alexander Nenashev Jun 13 '23 at 11:54
  • Thank You @AlexanderNenashev, but I have decided to stay with vue-cli. Don't want to change code for a build tool - at least not know – jimmybondy Jun 16 '23 at 16:31

0 Answers0