Decomment
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
-
Error: Line X: Unexpected token
cause The input JavaScript or JSON code contains syntax errors or non-standard constructs that Esprima cannot parse strictly.fixCorrect the syntax errors in your code. For less strict parsing, upgrade to `decomment` v0.9.4 or higher and use `decomment(code, { tolerant: true })`. -
TypeError: decomment is not a function
cause Attempting to import `decomment` using a named import or an incorrect CommonJS `require` pattern, when it is the default/module.exports.fixFor CommonJS, use `const decomment = require('decomment');`. For ESM, use `import decomment from 'decomment';`.
Warnings
- breaking Support for Node.js versions 0.10 and 0.12 was dropped starting with version 0.9.0. The last version to support these legacy environments was 0.8.8.
- gotcha The library does not support mixed content. If the input is recognized as HTML, only HTML comments (`<!-- -->`) will be removed, ignoring any JavaScript or CSS comments embedded within script or style tags.
- gotcha By default, Decomment performs strict JavaScript parsing via Esprima. If the input JavaScript or JSON code is syntactically invalid, the parser will throw an error.
- gotcha The `tolerant` option, which allows for less strict JavaScript parsing, was introduced in version 0.9.4. Prior versions perform strictly compliant parsing.
Install
-
npm install decomment -
yarn add decomment -
pnpm add decomment
Imports
- decomment
const decomment = require('decomment').decomment;import decomment from 'decomment';
- decomment
const decomment = require('decomment');
Quickstart
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);