Nov 1, 2022
Find duplicate code between two folders in a project
A CLI tool to quickly identify duplicate code.
- refactor
- cli
I needed to identify common code between 2 folders of the same project.
Instead of checking all the files manually one by one, I used a tool that made the task easier: jscpd.
It's an NPM package that you can install in your project, but in my case I used the CLI version.
If I want to find the code in common between the ./folder1 and ./folder2 folders in my next.js project, want to ignore the code compiled in ./.next and the ./node_modules, and want an html report, I position myself at the root of my project, and run the following command:
npx jscpd --skipLocal ./folder1 ./folder2 --reporters html --ignore "**/node_modules,**/.next"
Then you just have to open the file ./report/html/index.html in a browser to see the report.
Be careful, you must remember to delete the report and not commit it!

Settings#
We can also change the basic parameters according to our needs. The documentation lists all the options.
For example, if we take the following command line:
npx jscpd --pattern "src/**/*.js" --min-tokens 15 --min-lines 5
--patternor-pallows us to target only the files corresponding to a given pattern. Here we will target all javascript files in thesrcfolder.--min-tokensor-kis the minimum number of token repetitions. A token is the equivalent of a word. Here, an extract of code will only be detected as duplicated if it has 15 or more identical tokens.--min-linesor-lis the minimum number of lines of code to detect. Here, an extract of code will be detected as duplicated only from 15 identical lines.