JavaScript API for npm dependency checks
The JavaScript API runs the same check as the CLI and returns the result as a typed object, so you can call it in-process instead of starting outdated --json and parsing its output. The package is ESM-only and ships its own TypeScript declarations.
npm install --save-dev @ladamczyk/outdated
import { check, formatTable } from '@ladamczyk/outdated';
const result = await check({ cwd: '.', staleAfterMonths: 12, include: ['prod'] });
const deprecated = result.packages.filter((p) => p.flags.includes('deprecated'));
if (deprecated.length > 0) {
process.stdout.write(formatTable(result, { now: new Date() }));
process.exitCode = 1;
}
Exports
check(options?)— runs the check and resolves to anICheckResult, the object the CLI prints with--json.formatTable(result, { now, all?, color? })— renders the table the CLI prints.nowdrives the relative ages inLast publish;allalso lists ok rows;colordefaults to the terminal's support.formatJson(result)— renders the JSON the CLI prints: pretty-printed with a trailing newline.- Types:
ICheckOptions,ICheckResult,IPackageResult,ISummary,ISkipped,TRunner,TDepType,TFlag,TProblem.
check() options
All options are optional.
cwd— project folder, resolved to an absolute path. Default:process.cwd().staleAfterMonths— months without a release before a package is stale. Default:6.include— dependency types to check: any of'prod','dev','optional'. Default: all three.ignore— exact package names to leave out of the report.concurrency— parallelnpm viewlookups. Default:6.timeoutMs— timeout for eachnpm viewlookup. Default:30000. Not available as a CLI flag.runner— aTRunner,(args) => Promise<string>, that runsnpm <args>and resolves to its stdout. Default: the npm CLI. Inject one to test without the network.now— the clock used for staleness andgeneratedAt. Default:new Date().
The result
ICheckResult is the JSON output as a TypeScript type, schemaVersion: 3:
packages— oneIPackageResultper workspace and dependency, never filtered. Each row carriesflags, always in the orderdeprecated,stale,outdated,unknown,blocked, or exactly['ok'].summary— counts of each flag plustotalandskipped.skipped— dependencies that were not checked, each with areason.project,workspaces,generatedAtandstaleAfterMonthsdescribe the run.
TProblem is the set --fail-on accepts: deprecated, stale, outdated, unknown. blocked is informational and never a problem.
Errors
check() rejects when the project's package.json can't be read or parsed, when npm can't be started, or when npm outdated output can't be parsed. A single package whose npm view lookup fails or times out doesn't reject: its row gets the unknown flag and a reason.
check() never prompts and never sends usage statistics; those belong to the CLI only.
Used by qoq
qoq calls check() and formatTable() in-process for its dependency check. See qoq integration.