Skip to content

discoverPackageJson

Generated reference page for the discoverPackageJson function export.

Signatures

ts
function discoverPackageJson(adapter: PackageJsonAdapter, startDir?: string): Promise<PackageJsonData | null>;
ParameterTypeDescription
adapterPackageJsonAdapterAdapter providing readFile + cwd.
startDirstring | undefinedOptional explicit directory or file path to walk up from.
Pass an absolute path inside your own package (e.g.
fileURLToPath(import.meta.url)) when authoring an installable CLI whose
version should reflect the CLI's own package, not the consumer's working
directory. Defaults to adapter.cwd when undefined. A file path is
probed as a directory first (yielding nothing) before the walk-up reaches
its real parent directory, so passing import.meta.url resolves correctly.

Members

Members

discoverPackageJson

Discover the nearest package.json by walking up from startDir (or adapter.cwd when omitted).

Convenience helper behind CLIBuilder.packageJson(). Most apps should let the CLI runtime discover package metadata automatically; call this directly when testing metadata inference or embedding the behavior in custom tooling.

Returns the parsed metadata on success, null when no package.json is found (not an error). Malformed JSON and non-object roots also return null — the feature is a convenience, not a hard requirement.

ts
(adapter: PackageJsonAdapter, startDir?: string): Promise<PackageJsonData | null>;

Examples

ts
import { discoverPackageJson } from '@kjanat/dreamcli';

const pkg = await discoverPackageJson(adapter);
if (pkg !== null) {
  console.log(pkg.version); // '1.2.3'
}

See Also

Released under the MIT License.