I'm using Next.js to develop a website.
I want to use dynamic import import()
to dynamically load a module. It might not exist.
And if it does not exist, I'm OK with supressing it:
const Blog = async () => {
let Layout = <div>Fallback layout</div>
try {
const { ModuleLayout } = await import('path-to-module').catch(reason => {})
if (ModuleLayout) {
Layout = ModuleLayout
}
}
catch (error) {
// intentionally swallawed
}
return <Layout />
}
I would expect the try-catch
statement to suppress the error for me.
But I get this error:
Module not found: Can't resolve 'path-to-module'
Why the catch
block does not work? How can I catch the error of dynamic import?