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

This commit is contained in:
Harlan Wilton 2025-02-20 23:44:13 +11:00 committed by GitHub
parent 15cf4d7466
commit 5fa257ad81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -79,6 +79,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
@ -118,9 +120,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
}