Remove every comment from modern JavaScript and TypeScript files with a single command. The CLI understands JSX, decorators, Flow, TypeScript, and the file extensions used across React, Next.js, and Node.js projects.
- Works with
.js,.cjs,.mjs,.jsx,.ts,.tsx,.cts,.mts(configurable via--extensions) - Accepts individual files or entire directories (recursively)
- Safe by default: dry-run mode plus explicit
--overwritegating for in-place edits - Write stripped sources somewhere else via
--out <file|dir> - JSON summaries for scripting plus a programmatic Node API
- Built on top of
@babel/parser, so it keeps pace with the latest language features
npm install --global decomment.js
# or
npx -p decomment.js decommentjs --helpUsage: decommentjs [options] <targets...>
Remove comments from modern JavaScript / TypeScript sources.
Arguments:
targets File(s) or directories to process.
Options:
-o, --out <path> Write stripped files to this path (file or directory).
--dry-run Preview the files that would be written without modifying anything.
--overwrite Allow overwriting the input files or existing outputs.
-e, --extensions <list> Comma-separated list of file extensions to include.
--json Emit a machine-readable JSON summary.
--silent Suppress human-readable logs (useful when piping JSON).
-h, --help Display help for command
Supported extensions: .js, .cjs, .mjs, .jsx, .ts, .tsx, .cts, .mts
# Preview what would change
decommentjs src --dry-run
# Overwrite files in place (requires explicit flag)
decommentjs src --overwrite
# Output to a sibling directory (creates it if needed)
decommentjs src --out dist/stripped
# Process a single file and write to another file
decommentjs component.jsx --out component.strip.jsx
# Emit JSON for scripting
decommentjs app --overwrite --json --silent > report.json
# Limit processed extensions
decommentjs src --extensions .js,.jsx --overwrite- Directories are traversed recursively; symbolic links are skipped to avoid accidental loops.
- Only files whose extension matches the allowlist are read or written.
- When
--outis set:- Directories mirror the source tree structure.
- Files are written even if no comments were removed so the output tree is complete.
- Without
--out, the tool requires--overwriteto avoid accidental mutation. --jsonreturns{ summaries: [...], totals: { ... } }, mirroring the CLI stats.
const { stripCommentsFromCode, stripPath } = require('decomment.js');Removes every comment node from the provided string.
Returns:
{
code: string; // comment-free source
commentCount: number; // number of removed comments
removedChars: number; // total character count removed
removedRanges: Array<{ start: number; end: number }>;
}You can pass any @babel/parser option (for example a custom plugins array) via parserOptions.
Strips an individual file or an entire directory tree.
Options:
| Option | Type | Default | Description |
|---|---|---|---|
dryRun |
boolean | false |
Collect stats without writing files. |
overwrite |
boolean | false |
Required for in-place edits or when replacing existing outputs. |
outPath |
string | undefined |
Write results to this file or directory. Required if overwrite is false and you’re not in dry-run mode. |
extensions |
string | string[] | Set | built-in list | Override the extension allowlist (provide .js-style entries). |
parserPlugins |
string[] | internal presets | Override the parser plugin list. |
Result:
{
target: string;
outputBase: string | null;
filesScanned: number;
filesChanged: number;
commentsRemoved: number;
bytesRemoved: number;
results: Array<{
inputPath: string;
outputPath: string;
commentCount: number;
removedChars: number;
changed: boolean;
wroteFile: boolean;
}>;
}git clone <repo>
cd decomment.js
npm install
npm testThe test suite performs both unit-level (stripCommentsFromCode) and integration (stripPath) checks using temporary files.