fix(kit): expose module dependency errors (#31035)

This commit is contained in:
Harlan Wilton 2025-02-20 23:44:13 +11:00 committed by Daniel Roe
parent d992c0da95
commit bb1a376558
No known key found for this signature in database
GPG Key ID: CBC814C393D93268

View File

@ -85,6 +85,8 @@ export const normalizeModuleTranspilePath = (p: string) => {
return getDirectory(p).split('node_modules/').pop() as string
}
const MissingModuleMatcher = /Cannot find module\s+['"]?([^'")\s]+)['"]?/i
export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, nuxt: Nuxt = useNuxt()) {
let buildTimeModuleMeta: ModuleMeta = {}
let resolvedModulePath: string | undefined
@ -124,9 +126,17 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n
break
} catch (error: unknown) {
const code = (error as Error & { code?: string }).code
if (code === 'MODULE_NOT_FOUND' || code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' || code === 'ERR_MODULE_NOT_FOUND' || code === 'ERR_UNSUPPORTED_DIR_IMPORT' || code === 'ENOTDIR') {
if (code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' || code === 'ERR_UNSUPPORTED_DIR_IMPORT' || code === 'ENOTDIR') {
continue
}
if (code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND') {
const module = MissingModuleMatcher.exec((error as Error).message)?.[1]
// verify that it's missing the nuxt module otherwise it may be a sub dependency of the module itself
// i.e module is importing a module that is missing
if (!module || module.includes(nuxtModule as string)) {
continue
}
}
logger.error(`Error while importing module \`${nuxtModule}\`: ${error}`)
throw error
}