Skip to content

Commit 79a829d

Browse files
committed
fix: don't mistake another package.json for nuxt's own
1 parent d0b43f8 commit 79a829d

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

packages/nuxt-cli/src/commands/module/search.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { kebabCase, upperFirst } from 'scule'
99
import { formatInfoBox } from '../../utils/formatting'
1010
import { logger } from '../../utils/logger'
1111
import { logNetworkError } from '../../utils/network'
12-
import { getNuxtVersion } from '../../utils/versions'
12+
import { DEFAULT_NUXT_VERSION, getNuxtVersion } from '../../utils/versions'
1313
import { cwdArgs } from '../_shared'
1414
import { checkNuxtCompatibility, fetchModules, MODULES_API_URL } from './_utils'
1515

@@ -41,7 +41,7 @@ export default defineCommand({
4141
},
4242
},
4343
async setup(ctx) {
44-
const nuxtVersion = await getNuxtVersion(ctx.args.cwd)
44+
const nuxtVersion = await getNuxtVersion(ctx.args.cwd).catch(() => DEFAULT_NUXT_VERSION)
4545
return findModuleByKeywords(ctx.args._.join(' '), nuxtVersion)
4646
},
4747
})

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ import { coerce } from 'verkit'
55

66
import { tryResolveNuxt } from './kit'
77

8+
/**
9+
* Names a resolved `nuxt` dependency can legitimately have, so that a
10+
* `package.json` reached through `pkg-types`' nearest-file fallback (which
11+
* happens when `nuxt` cannot be resolved at all) is not mistaken for Nuxt's own.
12+
*/
13+
const NUXT_PACKAGE_NAMES = new Set(['nuxt', 'nuxt-nightly', 'nuxt3', 'nuxt-edge'])
14+
15+
/** Assumed Nuxt version when the project declares no resolvable one. */
16+
export const DEFAULT_NUXT_VERSION = '3.0.0'
17+
818
export async function getNuxtVersion(cwd: string, cache = true) {
919
const nuxtPkg = await readPackageJSON('nuxt', { url: cwd, try: true, cache }).catch(() => null)
10-
if (nuxtPkg) {
11-
return nuxtPkg.version!
20+
if (nuxtPkg?.version && NUXT_PACKAGE_NAMES.has(nuxtPkg.name!)) {
21+
return nuxtPkg.version
1222
}
1323
const pkg = await readPackageJSON(cwd)
1424
const pkgDep = pkg?.dependencies?.nuxt || pkg?.devDependencies?.nuxt
15-
return (pkgDep && coerce(pkgDep)) || '3.0.0'
25+
return (pkgDep && coerce(pkgDep)) || DEFAULT_NUXT_VERSION
1626
}
1727

1828
export function getPkgVersion(cwd: string, pkg: string, options?: { via?: string[] }) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
2+
import { tmpdir } from 'node:os'
3+
import { join } from 'node:path'
4+
5+
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
6+
7+
import { getNuxtVersion } from '../../../src/utils/versions'
8+
9+
describe('getNuxtVersion', () => {
10+
let tempDir: string
11+
12+
beforeEach(async () => {
13+
tempDir = await mkdtemp(join(tmpdir(), 'nuxt-versions-test-'))
14+
})
15+
16+
afterEach(async () => {
17+
await rm(tempDir, { recursive: true, force: true })
18+
})
19+
20+
it('should read the declared version when nuxt is not installed', async () => {
21+
await writeFile(join(tempDir, 'package.json'), JSON.stringify({ devDependencies: { nuxt: '^4.2.0' } }))
22+
23+
expect(await getNuxtVersion(tempDir, false)).toBe('4.2.0')
24+
})
25+
})

0 commit comments

Comments
 (0)