Decomment

0.9.5 · active · verified Sun Apr 19

Decomment is a JavaScript utility library designed to efficiently remove comments from various source code and text formats including JSON, JavaScript, CSS, HTML, C++, and C. The current stable version is 0.9.5, reflecting a pre-1.0 development cadence. Releases primarily focus on dependency updates and minor feature enhancements. A key differentiator for JavaScript and JSON content is its reliance on the Esprima parser, which guarantees accurate comment removal even around complex syntax like regular expressions, distinguishing it from simpler regex-based comment removers. It preserves the original document's layout by optionally replacing comments with whitespace and specifically handles HTML comment syntax. The library explicitly states it does not support mixed content, meaning it won't remove JavaScript comments embedded within an HTML file, for instance. It demonstrates strong performance, capable of processing large files like AngularJS 1.5 Core (1.1MB) in under 100ms.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the `decomment` function to remove various types of comments from a JavaScript string, including preserving layout.

import decomment from 'decomment';

const jsCode = 'var t; // This is a single-line comment\n/*\n * This is a multi-line comment\n */\nconst obj = { /* inline */ key: 'value' };\n// Another line with a comment\nconsole.log(t);';

const cleanedCode = decomment(jsCode);
console.log('--- Original Code ---');
console.log(jsCode);
console.log('\n--- Cleaned Code (default) ---');
console.log(cleanedCode);
// Example with space option to preserve layout
const preservedLayoutCode = decomment(jsCode, { space: true });
console.log('\n--- Cleaned Code (space: true) ---');
console.log(preservedLayoutCode);

view raw JSON →