Skip to content

Commit e7aa760

Browse files
committed
fix: match exsolve resolution errors when @nuxt/kit is missing
1 parent 64b4525 commit e7aa760

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

packages/nuxt-cli/src/utils/kit.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { pathToFileURL } from 'node:url'
22
import { resolveModulePath } from 'exsolve'
33
import { withNodePath } from './paths'
44

5+
// `exsolve` and Node.js word their resolution failures differently
6+
const KIT_NOT_FOUND_RE = /Cannot (?:find|resolve) module ['"]@nuxt\/kit['"]/
7+
58
export async function loadKit(rootDir: string): Promise<typeof import('@nuxt/kit')> {
69
try {
710
const kitPath = resolveModulePath('@nuxt/kit', { from: tryResolveNuxt(rootDir) || rootDir })
@@ -18,7 +21,7 @@ export async function loadKit(rootDir: string): Promise<typeof import('@nuxt/kit
1821
return kit
1922
}
2023
catch (e: any) {
21-
if (e.toString().includes('Cannot find module \'@nuxt/kit\'')) {
24+
if (KIT_NOT_FOUND_RE.test(String(e))) {
2225
throw new Error(
2326
'nuxi requires `@nuxt/kit` to be installed in your project. Try installing `nuxt` v3+ or `@nuxt/bridge` first.',
2427
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { mkdtemp } from 'node:fs/promises'
2+
import { tmpdir } from 'node:os'
3+
import { join } from 'node:path'
4+
import process from 'node:process'
5+
6+
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
7+
8+
import { loadKit } from '../../../src/utils/kit'
9+
10+
let nodePath: string | undefined
11+
12+
// `vitest` points `NODE_PATH` at the pnpm store, where `@nuxt/kit` is resolvable
13+
beforeEach(() => {
14+
nodePath = process.env.NODE_PATH
15+
delete process.env.NODE_PATH
16+
})
17+
18+
afterEach(() => {
19+
process.env.NODE_PATH = nodePath
20+
})
21+
22+
describe('loadKit', () => {
23+
it('should explain how to install `@nuxt/kit` when it cannot be resolved', async () => {
24+
const rootDir = await mkdtemp(join(tmpdir(), 'nuxi-kit-'))
25+
26+
await expect(loadKit(rootDir)).rejects.toThrowError(
27+
'nuxi requires `@nuxt/kit` to be installed in your project. Try installing `nuxt` v3+ or `@nuxt/bridge` first.',
28+
)
29+
})
30+
})

0 commit comments

Comments
 (0)