You can’t catch static imports errors (cf. Boris’ answer)
Yet, you could use a dynamic import()
for that.
It’s now supported by all evergreen browsers & Node, and is part of the standards since ES2020.
class ImportError extends Error {}
const loadModule = async (modulePath) => {
try {
return await import(modulePath)
} catch (e) {
throw new ImportError(`Unable to import module ${modulePath}`)
}
}