Node.js bindings
Call the fallow analyses from Node.js code with @fallow-cli/fallow-node and read the same JSON reports as the CLI.
You build a tool in Node.js, for example a dashboard, a code review bot or a release script, and it needs the findings of fallow. You can start the CLI as a child process and parse its standard output, but then your tool must find the binary and handle the process. The package @fallow-cli/fallow-node runs the analyses inside your Node.js process instead. Each function returns the same report as the CLI with --format json, as a JavaScript object.
Use the bindings in a tool, a server or a script that runs in Node.js. For a shell script or a CI job, use the CLI.
Before you start
- Node.js 22 or later.
- A JavaScript or TypeScript project with its dependencies installed.
Steps
-
Install the package:
npm install @fallow-cli/fallow-nodeThe package is a native addon. npm installs the binary for your platform: macOS, Linux (glibc or musl) or Windows, on x64 or arm64.
-
Write a script that calls the analyses. This script prints the dead code summary, each unused export and the health score:
// report.mjs import { detectDeadCode, computeHealth } from "@fallow-cli/fallow-node"; const root = process.argv[2] ?? process.cwd(); const deadCode = await detectDeadCode({ root }); console.log(`${deadCode.kind}: ${deadCode.summary.total_issues} issues`); for (const item of deadCode.unused_exports) { console.log(`unused export ${item.path}:${item.line} ${item.export_name}`); } const health = await computeHealth({ root, score: true }); console.log(`health score ${health.health_score.score} (${health.health_score.grade})`);An ES module can import each function by name, as in the example, or import the default export with all functions. In CommonJS, use
require("@fallow-cli/fallow-node"). -
Run the script in the project root.
Check the result
In the demo project, the script prints the findings that fallow dead-code and fallow health --score report:
node report.mjs
dead-code: 6 issues
unused export src/lib/format.ts:3 formatPercent
unused export src/lib/index.ts:1 formatPercent
unused export src/services/orders.ts:18 cancelOrder
health score 60.8 (C)
The 6 issues are all dead code findings, also the unused file and the two dependency findings. The script prints only the unused exports. Each report has a kind field that names the analysis, and a schema_version. The paths are relative to the root. JSON output describes each report.
If it fails
A function does not exit your process on an error. It rejects with a FallowNodeError, which has the fields message, exitCode, and optionally code, help and context:
try {
await detectDeadCode({ root: "/does/not/exist" });
} catch (error) {
console.log(error.code, error.exitCode);
}
FALLOW_INVALID_ROOT 2
The exit codes are the same as on the CLI. See Exit codes.
Cannot find native binding. npm skipped the optional dependencies, ornode_modulescomes from another operating system. Runnpm installagain on the machine that runs the script.
Reference
Functions
Each function is async and takes an optional options object.
| Function | CLI equivalent | Returns |
|---|---|---|
detectDeadCode(options?) | fallow dead-code --format json | DeadCodeReport |
detectCircularDependencies(options?) | fallow dead-code --circular-deps --format json | DeadCodeReport with only circular dependencies |
detectBoundaryViolations(options?) | fallow dead-code --boundary-violations --format json | DeadCodeReport with only boundary violations |
detectArchitecture(options?) | fallow architecture --format json | ArchitectureReport |
detectDuplication(options?) | fallow dupes --format json | DuplicationReport |
detectSimilarCode(options?) | fallow similar-code --format json | SimilarCodeReport |
detectFeatureFlags(options?) | fallow flags --format json | FeatureFlagsReport |
computeComplexity(options?) | fallow health --format json | HealthReport |
computeHealth(options?) | fallow health --format json | The same as computeComplexity |
detectSimilarCode needs the local model of similar-code analysis. It never downloads the model. Run fallow similar-code setup --local once with the CLI.
Shared options
Each options object accepts these fields. An enum value uses the lowercase value of the CLI, for example "mild".
| Field | Type | CLI equivalent |
|---|---|---|
root | string | --root. The default is process.cwd(). |
configPath | string | --config |
allowRemoteExtends | boolean | --allow-remote-extends |
noCache | boolean | --no-cache |
threads | number | --threads |
diffFile | string | --diff-file. A path only: the bindings do not read a diff from stdin. |
production | boolean | --production |
changedSince | string | --changed-since |
workspace | string[] | --workspace |
changedWorkspaces | string | --changed-workspaces |
explain | boolean | --explain |
Each function also takes the options of its command, with the CLI option in camel case: for example unusedExports for --unused-exports, minTokens for --min-tokens, or score for --score. The package types in types/index.d.ts list each option of each function.
The bindings run the analyses only. They have no function that writes files, for example fix, init or --save-baseline, and no watch mode.