diff --git a/src/commands/build-module.ts b/src/commands/build-module.ts index 6f8c5f395..3a870d1d5 100644 --- a/src/commands/build-module.ts +++ b/src/commands/build-module.ts @@ -34,7 +34,7 @@ export default defineCommand({ // Find local installed version const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) - const hasLocal = await readPackageJSON(MODULE_BUILDER_PKG, { url: cwd }) + const hasLocal = await readPackageJSON(MODULE_BUILDER_PKG, { url: cwd }).catch(() => false) const execArgs = Object.entries({ '--stub': ctx.args.stub, diff --git a/src/commands/info.ts b/src/commands/info.ts index d6c58ebd6..111c39f7d 100644 --- a/src/commands/info.ts +++ b/src/commands/info.ts @@ -1,4 +1,5 @@ import type { NuxtConfig, NuxtModule } from '@nuxt/schema' +import type { PackageJson } from 'pkg-types' import os from 'node:os' import process from 'node:process' @@ -35,7 +36,7 @@ export default defineCommand({ const nuxtConfig = await getNuxtConfig(cwd) // Find nearest package.json - const { dependencies = {}, devDependencies = {} } = await readPackageJSON(cwd) + const { dependencies = {}, devDependencies = {} } = await readPackageJSON(cwd).catch(() => ({} as PackageJson)) // Utils to query a dependency version const nuxtPath = await tryResolveNuxt(cwd) diff --git a/src/commands/module/_utils.ts b/src/commands/module/_utils.ts index 242fd7e18..e10993bff 100644 --- a/src/commands/module/_utils.ts +++ b/src/commands/module/_utils.ts @@ -116,7 +116,7 @@ export function checkNuxtCompatibility( } export async function getNuxtVersion(cwd: string) { - const nuxtPkg = await readPackageJSON('nuxt', { url: cwd }) + const nuxtPkg = await readPackageJSON('nuxt', { url: cwd }).catch(() => null) if (nuxtPkg) { return nuxtPkg.version! } diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 4a39858ae..083e8db75 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -1,11 +1,12 @@ import type { FileHandle } from 'node:fs/promises' -import type { NuxtModule } from './_utils' +import type { PackageJson } from 'pkg-types' +import type { NuxtModule } from './_utils' import * as fs from 'node:fs' import { homedir } from 'node:os' import { join } from 'node:path' -import process from 'node:process' +import process from 'node:process' import { updateConfig } from 'c12/update' import { defineCommand } from 'citty' import { colors } from 'consola/utils' @@ -63,7 +64,7 @@ export default defineCommand({ async setup(ctx) { const cwd = resolve(ctx.args.cwd) const modules = ctx.args._.map(e => e.trim()).filter(Boolean) - const projectPkg = await readPackageJSON(cwd) + const projectPkg = await readPackageJSON(cwd).catch(() => ({} as PackageJson)) if (!projectPkg.dependencies?.nuxt && !projectPkg.devDependencies?.nuxt) { logger.warn(`No \`nuxt\` dependency detected in \`${cwd}\`.`) diff --git a/src/utils/banner.ts b/src/utils/banner.ts index 328654f0d..635800827 100644 --- a/src/utils/banner.ts +++ b/src/utils/banner.ts @@ -12,7 +12,7 @@ export async function showVersions(cwd: string) { if (!url) { continue } - const p = await readPackageJSON(pkg, { url }) + const p = await readPackageJSON(pkg, { url }).catch(() => null) if (p) { return p.version! }