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.
npm install decommentVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of the `decomment` function to remove various types of comments from a JavaScript string, including preserving layout.
Upgrade your Node.js environment to version 4, 6, 8, or newer (minimum Node.js 6.4 required).
For mixed content, consider using separate parsing steps for each language or a more specialized tool that understands embedded languages.
Ensure your input code is valid JavaScript or JSON. Alternatively, use the `tolerant: true` option (available since 0.9.4) to potentially handle some non-strict constructs or errors more gracefully, though this might not guarantee full parsing.
If encountering parsing errors with non-strict JavaScript in older versions, upgrade to >=0.9.4 and use the `{ tolerant: true }` option.Correct the syntax errors in your code. For less strict parsing, upgrade to `decomment` v0.9.4 or higher and use `decomment(code, { tolerant: true })`.For CommonJS, use `const decomment = require('decomment');`. For ESM, use `import decomment from 'decomment';`.