diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index a55f391c13..cd2ebea068 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -1,4 +1,6 @@ +import { lstatSync } from 'node:fs' import type { Nuxt, NuxtModule } from '@nuxt/schema' +import { dirname, isAbsolute } from 'pathe' import { isNuxt2 } from '../compatibility' import { useNuxt } from '../context' import { requireModule, resolveModule } from '../internal/cjs' @@ -22,7 +24,7 @@ export async function installModule (moduleToInstall: string | NuxtModule, _inli } if (typeof moduleToInstall === 'string') { - nuxt.options.build.transpile.push(moduleToInstall) + nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleToInstall)) } nuxt.options._installedModules = nuxt.options._installedModules || [] @@ -35,6 +37,17 @@ export async function installModule (moduleToInstall: string | NuxtModule, _inli // --- Internal --- +export const normalizeModuleTranspilePath = (p: string) => { + try { + // we need to target directories instead of module file paths themselves + // /home/user/project/node_modules/module/index.js -> /home/user/project/node_modules/module + p = isAbsolute(p) && lstatSync(p).isFile() ? dirname(p) : p + } catch (e) { + // maybe the path is absolute but does not exist, allow this to bubble up + } + return p.split('node_modules/').pop() as string +} + async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?: any) { const nuxt = useNuxt() diff --git a/packages/nuxt/src/core/modules.ts b/packages/nuxt/src/core/modules.ts index 6df928fc7d..928e58edc6 100644 --- a/packages/nuxt/src/core/modules.ts +++ b/packages/nuxt/src/core/modules.ts @@ -1,4 +1,4 @@ -import { useNuxt } from '@nuxt/kit' +import { normalizeModuleTranspilePath, useNuxt } from '@nuxt/kit' export interface AddModuleTranspilesOptions { additionalModules?: string[] @@ -14,7 +14,7 @@ export const addModuleTranspiles = (opts: AddModuleTranspilesOptions = {}) => { ] .map(m => typeof m === 'string' ? m : Array.isArray(m) ? m[0] : m.src) .filter(m => typeof m === 'string') - .map(m => m.split('node_modules/').pop()) + .map(normalizeModuleTranspilePath) // Try to sanitize modules to better match imports nuxt.options.build.transpile =