API
API
Use jscpd programmatically in your applications.
Overview
jscpd provides several integration options depending on your version:
| v5 (Rust) | v4 (TypeScript) | |
|---|---|---|
| CLI | Native binary — fastest option | Node.js CLI |
| Programmatic API | Rust crates (cpd-finder) | Node.js (import { jscpd }) |
| Server | jscpd-server (Node.js) | jscpd-server (Node.js) |
v5: Rust Crate API
The core detection engine is available as Rust crates:
Cargo.toml
[dependencies]
cpd-finder = "0.1"
src/main.rs
use cpd_finder::orchestrate;
fn main() {
let result = orchestrate(&["./src".into()], &Default::default());
println!("Clones: {}", result.statistics.total_clones);
println!("Duplication: {:.1}%", result.statistics.total_percentage);
}
Available Crates
| Crate | Description |
|---|---|
| cpd | CLI entry point (produces both jscpd and cpd binaries) |
| cpd-core | Core detection algorithm (Rabin-Karp rolling hash) |
| cpd-tokenizer | Source code tokenizer (OXC for JS/TS, generic for 223 formats) |
| cpd-finder | File walking, orchestration, git blame |
| cpd-reporter | 13 output reporters |
See Rust Crates for full documentation.
Rust Crate Features
- Pure Rust — no external runtime required
- Parallel detection across format groups via
rayon - OXC-based JS/TS/JSX/TSX tokenization
gitoxide-based in-process git blame (--blame)- Cross-format detection for Vue, Svelte, Astro, Markdown
v4: Node.js API
If you need a JavaScript/TypeScript programmatic API, use jscpd@4:
import { IClone } from '@jscpd/core';
import { jscpd } from 'jscpd';
// Promise API
const clones: Promise<IClone[]> = jscpd(process.argv);
// Async/Await API
(async () => {
const clones: IClone[] = await jscpd(['', '', __dirname, '-r', 'json']);
console.log(clones);
})();
Options
Pass options programmatically:
import { jscpd } from 'jscpd';
const options = [
'', // placeholder for node
'', // placeholder for script
'./src',
'--min-lines', '5',
'--min-tokens', '50',
'--reporters', 'json',
'--output', './reports'
];
const clones = await jscpd(options);
Clone Interface
Each detected clone has the following structure:
interface IClone {
format: string;
lines: number;
tokens: number;
firstFile: ISourceLocation;
secondFile: ISourceLocation;
duplicationA: ISourceLines;
duplicationB: ISourceLines;
}
interface ISourceLocation {
name: string;
start: number;
end: number;
startLoc: ILocation;
endLoc: ILocation;
}
Note: The v4 Node.js API is not available in v5. If you're migrating, see the Migration Guide for alternatives.
CLI
The simplest integration is via the command line (same in both versions):
jscpd ./src --reporters json --output ./reports
See Installation and Configuration for full CLI options.
Server API
For REST API and MCP integration, see the Server API and MCP Server pages.