website/client/src/en/guide/development/using-repomix-as-a-library.md
In addition to using Repomix as a CLI tool, you can integrate its functionality directly into your Node.js applications.
Install Repomix as a dependency in your project:
npm install repomix
The simplest way to use Repomix is through the runCli function, which provides the same functionality as the command-line interface:
import { runCli, type CliOptions } from 'repomix';
// Process current directory with custom options
async function packProject() {
const options = {
output: 'output.xml',
style: 'xml',
compress: true,
quiet: true
} as CliOptions;
const result = await runCli(['.'], process.cwd(), options);
return result.packResult;
}
The result.packResult contains information about the processed files, including:
totalFiles: Number of files processedtotalCharacters: Total character counttotalTokens: Total token count (useful for LLM context limits)fileCharCounts: Character counts per filefileTokenCounts: Token counts per fileYou can clone and process a remote repository:
import { runCli, type CliOptions } from 'repomix';
// Clone and process a GitHub repo
async function processRemoteRepo(repoUrl) {
const options = {
remote: repoUrl,
output: 'output.xml',
compress: true
} as CliOptions;
return await runCli(['.'], process.cwd(), options);
}
[!NOTE] For security, config files in remote repositories are not loaded by default. To trust a remote repository's config, add
remoteTrustConfig: trueto the options, or set theREPOMIX_REMOTE_TRUST_CONFIG=trueenvironment variable.
For more control, you can use Repomix's low-level APIs directly:
import { searchFiles, collectFiles, processFiles, TokenCounter } from 'repomix';
async function analyzeFiles(directory) {
// Find and collect files
const { filePaths } = await searchFiles(directory, { /* config */ });
const rawFiles = await collectFiles(filePaths, directory);
const processedFiles = await processFiles(rawFiles, { /* config */ });
// Count tokens
const tokenCounter = new TokenCounter('o200k_base');
// Return analysis results
return processedFiles.map(file => ({
path: file.path,
tokens: tokenCounter.countTokens(file.content)
}));
}
When bundling repomix with tools like Rolldown or esbuild, some dependencies must remain external and WASM files need to be copied:
External dependencies (cannot be bundled):
tinypool - Spawns worker threads using file pathsWASM files to copy:
web-tree-sitter.wasm → Same directory as bundled JS (required for code compression feature)REPOMIX_WASM_DIR environment variableFor a working example, see website/server/scripts/bundle.mjs.
The Repomix website (repomix.com) uses Repomix as a library to process remote repositories. You can see the implementation in website/server/src/remoteRepo.ts.