fix(kit): try resolving module path from each node_modules dir

This commit is contained in:
Daniel Roe 2024-09-20 13:29:52 +01:00
parent 33e2d0a5f3
commit 097105fb2b
No known key found for this signature in database
GPG Key ID: CBC814C393D93268

View File

@ -79,23 +79,26 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n
// Import if input is string // Import if input is string
if (typeof nuxtModule === 'string') { if (typeof nuxtModule === 'string') {
const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule, join(nuxt.options.rootDir, nuxtModule)]
let error: unknown let error: unknown
for (const path of paths) { const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule, join(nuxt.options.rootDir, nuxtModule)]
try {
const src = jiti.esmResolve(path)
nuxtModule = await jiti.import(src) as NuxtModule
// nuxt-module-builder generates a module.json with metadata including the version for (const parentURL of nuxt.options.modulesDir) {
const moduleMetadataPath = join(dirname(src), 'module.json') for (const path of paths) {
if (existsSync(moduleMetadataPath)) { try {
buildTimeModuleMeta = JSON.parse(await fsp.readFile(moduleMetadataPath, 'utf-8')) const src = jiti.esmResolve(path, { parentURL })
nuxtModule = await jiti.import(src) as NuxtModule
// nuxt-module-builder generates a module.json with metadata including the version
const moduleMetadataPath = join(dirname(src), 'module.json')
if (existsSync(moduleMetadataPath)) {
buildTimeModuleMeta = JSON.parse(await fsp.readFile(moduleMetadataPath, 'utf-8'))
}
break
} catch (_err: unknown) {
error = _err
} }
break
} catch (_err: unknown) {
error = _err
continue
} }
if (typeof nuxtModule !== 'string') { break }
} }
if (typeof nuxtModule !== 'function' && error) { if (typeof nuxtModule !== 'function' && error) {
logger.error(`Error while importing module \`${nuxtModule}\`: ${error}`) logger.error(`Error while importing module \`${nuxtModule}\`: ${error}`)