Surrounding try catch block around require keyword

Keywords´╝Ü javascript node.js reactjs


I have a function in one of my files that the user can call with 2 parameters. These parameters are then used to construct a file path and if a file exists in that path then it should get that, else it should resort into a default file.

export const formImporter = (book, author) => {

  const importedFile = require(`../../chapter/page/${book}/${author}.jsx`).default
    || require(`../../chapter/page/common/${author}.jsx`).default;

  return importedFile; };

These files are functional react components. The problem is that I can't surround this with a try catch and this method doesn't work as it fails on the first case and does not bother trying the with the second case.

Essentially, I'd like for it to check if the path with the book and author exists. If not, then default to the author path.