Remove all comments from modern JavaScript and TypeScript files via a simple CLI.
npm install decomment.jsRemove 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.
.js, .cjs, .mjs, .jsx, .ts, .tsx, .cts, .mts (configurable via --extensions)--overwrite gating for in-place edits--out @babel/parser, so it keeps pace with the latest language features---
``bash`
npm install --global decomment.jsor
npx -p decomment.js decommentjs --help
---
`
Usage: decommentjs [options]
Remove comments from modern JavaScript / TypeScript sources.
Arguments:
targets File(s) or directories to process.
Options:
-o, --out
--dry-run Preview the files that would be written without modifying anything.
--overwrite Allow overwriting the input files or existing outputs.
-e, --extensions 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
`
`bashPreview what would change
decommentjs src --dry-run
$3
- Directories are traversed recursively; symbolic links are skipped to avoid accidental loops.
- Only files whose extension matches the allowlist are read or written.
- When --out is 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 --overwrite to avoid accidental mutation.
- --json returns { summaries: [...], totals: { ... } }, mirroring the CLI stats.---
Node.js API
`js
const { stripCommentsFromCode, stripPath } = require('decomment.js');
`$3
Removes every comment node from the provided string.
Returns:
`ts
{
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.$3
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:
`ts
{
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;
}>;
}
`---
Development
`bash
git clone
cd decomment.js
npm install
npm test
`The test suite performs both unit-level (
stripCommentsFromCode) and integration (stripPath`) checks using temporary files.---