The jscpd is an open-source copy/paste detector designed to identify duplicated code across source files and projects. It supports multiple programming languages and can help locate repeated code that may be worth refactoring. This tutorial explains how to install jscpd on Ubuntu 26.04.
Install jscpd
Download the latest release from the GitHub repository and extract the binary directly into /usr/local/bin:
curl -sSL https://github.com/kucherenko/jscpd/releases/latest/download/jscpd-linux-x64-gnu.tar.gz \
| sudo tar xz -C /usr/local/bin jscpd
Check the jscpd version to verify that the binary is available:
jscpd --version
Testing jscpd
Create a small JavaScript file containing two functions with intentionally repeated code:
nano test.js
Add the following content:
function processUser(name, age, email, country, city) {
const user = {
name,
age,
email,
country,
city,
};
const json = JSON.stringify(user);
console.log('Processing user', json);
return json;
}
function processCustomer(name, age, email, country, city) {
const user = {
name,
age,
email,
country,
city,
};
const json = JSON.stringify(user);
console.log('Processing user', json);
return json;
}
Run jscpd against the file:
jscpd test.js
The output should look similar to the following:
Clone found (javascript)
- test.js [1:21 - 14:2] (14 lines, 51 tokens)
test.js [16:25 - 29:2]
┌────────────┬────────────────┬─────────────┬──────────────┬──────────────┬──────────────────┬───────────────────┐
│ Format │ Files analyzed │ Total lines │ Total tokens │ Clones found │ Duplicated lines │ Duplicated tokens │
├────────────┼────────────────┼─────────────┼──────────────┼──────────────┼──────────────────┼───────────────────┤
│ javascript │ 1 │ 29 │ 106 │ 1 │ 13 (44.83%) │ 51 (48.11%) │
├────────────┼────────────────┼─────────────┼──────────────┼──────────────┼──────────────────┼───────────────────┤
│ Total: │ 1 │ 29 │ 106 │ 1 │ 13 (44.83%) │ 51 (48.11%) │
└────────────┴────────────────┴─────────────┴──────────────┴──────────────┴──────────────────┴───────────────────┘
Found 1 clones.
time: 3.111ms
The report identifies one duplicated section in the JavaScript file, with 13 lines of code detected as duplicates. This highlights the repeated section that could potentially be simplified during refactoring.
Uninstall jscpd
If jscpd is no longer needed, delete the installed binary by using the following command:
sudo rm -rf /usr/local/bin/jscpd
Leave a Comment
Cancel reply