diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index 31b1408426..9bcabb9f84 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -79,7 +79,6 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n // Import if input is string if (typeof nuxtModule === 'string') { - let error: unknown const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule, join(nuxt.options.rootDir, nuxtModule)] for (const parentURL of nuxt.options.modulesDir) { @@ -94,16 +93,17 @@ export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, n buildTimeModuleMeta = JSON.parse(await fsp.readFile(moduleMetadataPath, 'utf-8')) } break - } catch (_err: unknown) { - error = _err + } 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') { + continue + } + logger.error(`Error while importing module \`${nuxtModule}\`: ${error}`) + throw error } } if (typeof nuxtModule !== 'string') { break } } - if (typeof nuxtModule !== 'function' && error) { - logger.error(`Error while importing module \`${nuxtModule}\`: ${error}`) - throw error - } } // Throw error if input is not a function diff --git a/packages/vite/src/css.ts b/packages/vite/src/css.ts index 6457c78e43..4a26447584 100644 --- a/packages/vite/src/css.ts +++ b/packages/vite/src/css.ts @@ -27,11 +27,16 @@ export async function resolveCSSOptions (nuxt: Nuxt): Promise const pluginOptions = postcssOptions.plugins[pluginName] if (!pluginOptions) { continue } - const path = jiti.esmResolve(pluginName) - const pluginFn = (await jiti.import(path)) as (opts: Record) => Plugin - if (typeof pluginFn === 'function') { - css.postcss.plugins.push(pluginFn(pluginOptions)) - } else { + let pluginFn: ((opts: Record) => Plugin) | undefined + for (const parentURL of nuxt.options.modulesDir) { + pluginFn = await jiti.import(pluginName, { parentURL, try: true }) as (opts: Record) => Plugin + if (typeof pluginFn === 'function') { + css.postcss.plugins.push(pluginFn(pluginOptions)) + break + } + } + + if (typeof pluginFn !== 'function') { console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`) } } diff --git a/packages/webpack/src/utils/postcss.ts b/packages/webpack/src/utils/postcss.ts index 0d48972bb6..1548355eb0 100644 --- a/packages/webpack/src/utils/postcss.ts +++ b/packages/webpack/src/utils/postcss.ts @@ -49,11 +49,16 @@ export async function getPostcssConfig (nuxt: Nuxt) { const pluginOptions = postcssOptions.plugins[pluginName] if (!pluginOptions) { continue } - const path = jiti.esmResolve(pluginName) - const pluginFn = (await jiti.import(path)) as (opts: Record) => Plugin - if (typeof pluginFn === 'function') { - plugins.push(pluginFn(pluginOptions)) - } else { + let pluginFn: ((opts: Record) => Plugin) | undefined + for (const parentURL of nuxt.options.modulesDir) { + pluginFn = await jiti.import(pluginName, { parentURL, try: true }) as (opts: Record) => Plugin + if (typeof pluginFn === 'function') { + plugins.push(pluginFn(pluginOptions)) + break + } + } + + if (typeof pluginFn !== 'function') { console.warn(`[nuxt] could not import postcss plugin \`${pluginName}\`. Please report this as a bug.`) } }