{"id":10725,"library":"decomment","title":"Decomment","description":"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.","status":"active","version":"0.9.5","language":"javascript","source_language":"en","source_url":"https://github.com/vitaly-t/decomment","tags":["javascript","remove comments","JavaScript","JSON","CSS","HTML","CPP","C++","Text"],"install":[{"cmd":"npm install decomment","lang":"bash","label":"npm"},{"cmd":"yarn add decomment","lang":"bash","label":"yarn"},{"cmd":"pnpm add decomment","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for ECMAScript 6 compliance parsing of JavaScript and JSON content to accurately handle regular expressions and other complex syntax.","package":"esprima","optional":false}],"imports":[{"note":"The library primarily exports the main decomment function as the default export (CommonJS `module.exports` or ESM `default`).","wrong":"const decomment = require('decomment').decomment;","symbol":"decomment","correct":"import decomment from 'decomment';"},{"note":"This is the documented CommonJS import method for Node.js environments.","symbol":"decomment","correct":"const decomment = require('decomment');"}],"quickstart":{"code":"import decomment from 'decomment';\n\nconst 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);';\n\nconst cleanedCode = decomment(jsCode);\nconsole.log('--- Original Code ---');\nconsole.log(jsCode);\nconsole.log('\\n--- Cleaned Code (default) ---');\nconsole.log(cleanedCode);\n// Example with space option to preserve layout\nconst preservedLayoutCode = decomment(jsCode, { space: true });\nconsole.log('\\n--- Cleaned Code (space: true) ---');\nconsole.log(preservedLayoutCode);","lang":"javascript","description":"Demonstrates basic usage of the `decomment` function to remove various types of comments from a JavaScript string, including preserving layout."},"warnings":[{"fix":"Upgrade your Node.js environment to version 4, 6, 8, or newer (minimum Node.js 6.4 required).","message":"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.","severity":"breaking","affected_versions":">=0.9.0"},{"fix":"For mixed content, consider using separate parsing steps for each language or a more specialized tool that understands embedded languages.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If encountering parsing errors with non-strict JavaScript in older versions, upgrade to >=0.9.4 and use the `{ tolerant: true }` option.","message":"The `tolerant` option, which allows for less strict JavaScript parsing, was introduced in version 0.9.4. Prior versions perform strictly compliant parsing.","severity":"gotcha","affected_versions":"<0.9.4"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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 })`.","cause":"The input JavaScript or JSON code contains syntax errors or non-standard constructs that Esprima cannot parse strictly.","error":"Error: Line X: Unexpected token"},{"fix":"For CommonJS, use `const decomment = require('decomment');`. For ESM, use `import decomment from 'decomment';`.","cause":"Attempting to import `decomment` using a named import or an incorrect CommonJS `require` pattern, when it is the default/module.exports.","error":"TypeError: decomment is not a function"}],"ecosystem":"npm"}