From 26861e68ce8788868aaa69494dedec42bdcc90e4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 20 May 2023 23:10:52 +0100 Subject: [PATCH] fix(kit): resolve relative module paths when installing (#20896) --- packages/kit/src/module/install.ts | 8 ++++++-- test/fixtures/basic/modules/test/index.ts | 7 +++++++ test/fixtures/basic/nuxt.config.ts | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/basic/modules/test/index.ts diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index 0cce502680..44da958f74 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -1,6 +1,6 @@ import { lstatSync } from 'node:fs' import type { Nuxt, NuxtModule } from '@nuxt/schema' -import { dirname, isAbsolute, normalize } from 'pathe' +import { dirname, isAbsolute, normalize, resolve } from 'pathe' import { isNuxt2 } from '../compatibility' import { useNuxt } from '../context' import { requireModule } from '../internal/cjs' @@ -53,7 +53,11 @@ async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?: // Import if input is string if (typeof nuxtModule === 'string') { - const src = normalize(resolveAlias(nuxtModule)) + let src = resolveAlias(nuxtModule) + if (src.match(/^\.{1,2}\//)) { + src = resolve(nuxt.options.rootDir, src) + } + src = normalize(src) try { // Prefer ESM resolution if possible nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir }) diff --git a/test/fixtures/basic/modules/test/index.ts b/test/fixtures/basic/modules/test/index.ts new file mode 100644 index 0000000000..b3c385cda4 --- /dev/null +++ b/test/fixtures/basic/modules/test/index.ts @@ -0,0 +1,7 @@ +import { defineNuxtModule } from 'nuxt/kit' + +export default defineNuxtModule({ + meta: { + name: 'test' + } +}) diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts index 7e9fdbca0a..0e588c0361 100644 --- a/test/fixtures/basic/nuxt.config.ts +++ b/test/fixtures/basic/nuxt.config.ts @@ -80,6 +80,7 @@ export default defineNuxtConfig({ } }, modules: [ + './modules/test/index', [ '~/modules/example', {