diff --git a/packages/nuxi/src/commands/build.ts b/packages/nuxi/src/commands/build.ts index 31d5a32c27..084eb637fb 100644 --- a/packages/nuxi/src/commands/build.ts +++ b/packages/nuxi/src/commands/build.ts @@ -1,8 +1,8 @@ import { resolve } from 'pathe' import consola from 'consola' -import { importModule } from '../utils/cjs' import { writeTypes } from '../utils/prepare' +import { loadKit } from '../utils/kit' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ @@ -15,7 +15,7 @@ export default defineNuxtCommand({ process.env.NODE_ENV = process.env.NODE_ENV || 'production' const rootDir = resolve(args._[0] || '.') - const { loadNuxt, buildNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit') + const { loadNuxt, buildNuxt } = await loadKit(rootDir) const nuxt = await loadNuxt({ rootDir }) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index dbf53c5a13..e8a489a061 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -5,8 +5,8 @@ import type { Nuxt } from '@nuxt/kit' import consola from 'consola' import { createServer, createLoadingHandler } from '../utils/server' import { showBanner } from '../utils/banner' -import { importModule } from '../utils/cjs' import { writeTypes } from '../utils/prepare' +import { loadKit } from '../utils/kit' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ @@ -26,7 +26,7 @@ export default defineNuxtCommand({ const rootDir = resolve(args._[0] || '.') - const { loadNuxt, buildNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit') + const { loadNuxt, buildNuxt } = await loadKit(rootDir) const prepare = debounce(nuxt => writeTypes(nuxt), 1000) diff --git a/packages/nuxi/src/commands/generate.ts b/packages/nuxi/src/commands/generate.ts index 62b37fc94a..f0a2e1a625 100644 --- a/packages/nuxi/src/commands/generate.ts +++ b/packages/nuxi/src/commands/generate.ts @@ -1,7 +1,7 @@ import { resolve } from 'pathe' import { isNuxt3 } from '@nuxt/kit' -import { importModule } from '../utils/cjs' +import { loadKit } from '../utils/kit' import { defineNuxtCommand } from './index' export default defineNuxtCommand({ @@ -14,7 +14,7 @@ export default defineNuxtCommand({ process.env.NODE_ENV = process.env.NODE_ENV || 'production' const rootDir = resolve(args._[0] || '.') - const { loadNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit') + const { loadNuxt } = await loadKit(rootDir) const nuxt = await loadNuxt({ rootDir }) if (isNuxt3(nuxt)) { diff --git a/packages/nuxi/src/commands/prepare.ts b/packages/nuxi/src/commands/prepare.ts index f09c40f354..bab6e7e68c 100644 --- a/packages/nuxi/src/commands/prepare.ts +++ b/packages/nuxi/src/commands/prepare.ts @@ -1,5 +1,5 @@ import { resolve } from 'pathe' -import { importModule } from '../utils/cjs' +import { loadKit } from '../utils/kit' import { writeTypes } from '../utils/prepare' import { defineNuxtCommand } from './index' @@ -13,7 +13,7 @@ export default defineNuxtCommand({ process.env.NODE_ENV = process.env.NODE_ENV || 'production' const rootDir = resolve(args._[0] || '.') - const { loadNuxt } = await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit') + const { loadNuxt } = await loadKit(rootDir) const nuxt = await loadNuxt({ rootDir }) await writeTypes(nuxt) diff --git a/packages/nuxi/src/utils/kit.ts b/packages/nuxi/src/utils/kit.ts new file mode 100644 index 0000000000..d6812cd17c --- /dev/null +++ b/packages/nuxi/src/utils/kit.ts @@ -0,0 +1,12 @@ +import { importModule } from './cjs' + +export const loadKit = async (rootDir: string): Promise => { + try { + return await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit') + } catch (e) { + 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 e + } +}