From 3b820ad366c63346d8daff0b9ccaf455b9726b10 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 10 May 2023 13:10:23 +0100 Subject: [PATCH] fix(kit): prefer esm resolution for modules to install (#20757) --- packages/kit/src/module/install.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index cd2ebea068..0cce502680 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -1,9 +1,9 @@ import { lstatSync } from 'node:fs' import type { Nuxt, NuxtModule } from '@nuxt/schema' -import { dirname, isAbsolute } from 'pathe' +import { dirname, isAbsolute, normalize } from 'pathe' import { isNuxt2 } from '../compatibility' import { useNuxt } from '../context' -import { requireModule, resolveModule } from '../internal/cjs' +import { requireModule } from '../internal/cjs' import { importModule } from '../internal/esm' import { resolveAlias } from '../resolve' @@ -53,12 +53,10 @@ async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?: // Import if input is string if (typeof nuxtModule === 'string') { - const _src = resolveModule(resolveAlias(nuxtModule), { paths: nuxt.options.modulesDir }) - // TODO: also check with type: 'module' in closest `package.json` - const isESM = _src.endsWith('.mjs') - + const src = normalize(resolveAlias(nuxtModule)) try { - nuxtModule = isESM ? await importModule(_src, nuxt.options.rootDir) : requireModule(_src) + // Prefer ESM resolution if possible + nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir }) } catch (error: unknown) { console.error(`Error while requiring module \`${nuxtModule}\`: ${error}`) throw error