mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
fix(kit): prefer esm resolution for modules to install (#20757)
This commit is contained in:
parent
32fa44c0c9
commit
3b820ad366
@ -1,9 +1,9 @@
|
|||||||
import { lstatSync } from 'node:fs'
|
import { lstatSync } from 'node:fs'
|
||||||
import type { Nuxt, NuxtModule } from '@nuxt/schema'
|
import type { Nuxt, NuxtModule } from '@nuxt/schema'
|
||||||
import { dirname, isAbsolute } from 'pathe'
|
import { dirname, isAbsolute, normalize } from 'pathe'
|
||||||
import { isNuxt2 } from '../compatibility'
|
import { isNuxt2 } from '../compatibility'
|
||||||
import { useNuxt } from '../context'
|
import { useNuxt } from '../context'
|
||||||
import { requireModule, resolveModule } from '../internal/cjs'
|
import { requireModule } from '../internal/cjs'
|
||||||
import { importModule } from '../internal/esm'
|
import { importModule } from '../internal/esm'
|
||||||
import { resolveAlias } from '../resolve'
|
import { resolveAlias } from '../resolve'
|
||||||
|
|
||||||
@ -53,12 +53,10 @@ async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?:
|
|||||||
|
|
||||||
// Import if input is string
|
// Import if input is string
|
||||||
if (typeof nuxtModule === 'string') {
|
if (typeof nuxtModule === 'string') {
|
||||||
const _src = resolveModule(resolveAlias(nuxtModule), { paths: nuxt.options.modulesDir })
|
const src = normalize(resolveAlias(nuxtModule))
|
||||||
// TODO: also check with type: 'module' in closest `package.json`
|
|
||||||
const isESM = _src.endsWith('.mjs')
|
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (error: unknown) {
|
||||||
console.error(`Error while requiring module \`${nuxtModule}\`: ${error}`)
|
console.error(`Error while requiring module \`${nuxtModule}\`: ${error}`)
|
||||||
throw error
|
throw error
|
||||||
|
Loading…
Reference in New Issue
Block a user