From 3684de58f4626e7b07dafdb0c3ddafaeaaccf811 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 11 Mar 2023 22:36:43 +0000 Subject: [PATCH] fix(nuxi): resolve kit from nuxt modules dir (#19601) --- packages/nuxi/src/utils/kit.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/nuxi/src/utils/kit.ts b/packages/nuxi/src/utils/kit.ts index c757c79257..9a6a8bff54 100644 --- a/packages/nuxi/src/utils/kit.ts +++ b/packages/nuxi/src/utils/kit.ts @@ -1,12 +1,24 @@ -import { importModule } from './esm' +import { importModule, tryResolveModule } from './esm' export const loadKit = async (rootDir: string): Promise => { try { - return await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit') + // Without PNP (or if users have a local install of kit, we bypass resolving from nuxt) + const localKit = await tryResolveModule('@nuxt/kit', rootDir) + // Otherwise, we resolve Nuxt _first_ as it is Nuxt's kit dependency that will be used + const rootURL = localKit ? rootDir : await tryResolveNuxt() || rootDir + return await importModule('@nuxt/kit', rootURL) as typeof import('@nuxt/kit') } catch (e: any) { if (e.toString().includes("Cannot find module '@nuxt/kit'")) { - throw new Error('nuxi requires `@nuxt/kit` to be installed in your project. Try installing `nuxt3` or `@nuxt/bridge` first.') + throw new Error('nuxi requires `@nuxt/kit` to be installed in your project. Try installing `nuxt` v3 or `@nuxt/bridge` first.') } throw e } } + +async function tryResolveNuxt () { + for (const pkg of ['nuxt3', 'nuxt', 'nuxt-edge']) { + const path = await tryResolveModule(pkg) + if (path) { return path } + } + return null +}