diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index c9f61b085f..c4a41470b0 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -20,11 +20,11 @@ export async function installModule< > (moduleToInstall: T, inlineOptions?: [Config] extends [never] ? any : Config[1], nuxt: Nuxt = useNuxt()) { const { nuxtModule, buildTimeModuleMeta, resolvedModulePath } = await loadNuxtModuleInstance(moduleToInstall, nuxt) - const localLayerModuleDirs = new Set() + const localLayerModuleDirs: string[] = [] for (const l of nuxt.options._layers) { const srcDir = l.config.srcDir || l.cwd if (!NODE_MODULES_RE.test(srcDir)) { - localLayerModuleDirs.add(resolve(srcDir, l.config?.dir?.modules || 'modules')) + localLayerModuleDirs.push(resolve(srcDir, l.config?.dir?.modules || 'modules').replace(/\/?$/, '/')) } } @@ -44,8 +44,8 @@ export async function installModule< const parsed = parseNodeModulePath(modulePath) const moduleRoot = parsed.dir ? parsed.dir + parsed.name : modulePath nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleRoot)) - const directory = parsed.dir ? moduleRoot : getDirectory(modulePath) - if (directory !== moduleToInstall && !localLayerModuleDirs.has(directory)) { + const directory = (parsed.dir ? moduleRoot : getDirectory(modulePath)).replace(/\/?$/, '/') + if (directory !== moduleToInstall && !localLayerModuleDirs.some(dir => directory.startsWith(dir))) { nuxt.options.modulesDir.push(resolve(directory, 'node_modules')) } } diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index fd4a9829d5..8e47db4014 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -403,8 +403,11 @@ async function initNuxt (nuxt: Nuxt) { ...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string), ) - // Ensure we can resolve dependencies within layers - nuxt.options.modulesDir.push(...nuxt.options._layers.map(l => resolve(l.cwd, 'node_modules'))) + // Ensure we can resolve dependencies within layers - filtering out local `~/layers` directories + const locallyScannedLayersDirs = nuxt.options._layers.map(l => resolve(l.cwd, 'layers').replace(/\/?$/, '/')) + nuxt.options.modulesDir.push(...nuxt.options._layers + .filter(l => l.cwd !== nuxt.options.rootDir && locallyScannedLayersDirs.every(dir => !l.cwd.startsWith(dir))) + .map(l => resolve(l.cwd, 'node_modules'))) // Init user modules await nuxt.callHook('modules:before') @@ -570,6 +573,7 @@ async function initNuxt (nuxt: Nuxt) { nuxt._ignore.add(resolveIgnorePatterns()) await nuxt.callHook('modules:done') + console.log(nuxt.options.modulesDir, nuxt.options.modulesDir.length) // Add if (nuxt.options.experimental.componentIslands) {