Registry / serialization / decomment

decomment

JSON →
library0.9.5jsnpmunverified

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 decomment
INSTALL
IMPORT
SIG · DECOMMENT
D
decomment
serializationjavascriptv0.9.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

decomment
import decomment from 'decomment';
const decomment = require('decomment').decomment;
The library primarily exports the main decomment function as the default export (CommonJS `module.exports` or ESM `default`).
decomment
const decomment = require('decomment');
This is the documented CommonJS import method for Node.js environments.

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);
Debug
Known issues
breakingSupport 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.
fix
Upgrade your Node.js environment to version 4, 6, 8, or newer (minimum Node.js 6.4 required).
affects: >=0.9.0
gotchaThe 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.
fix
For mixed content, consider using separate parsing steps for each language or a more specialized tool that understands embedded languages.
affects: >=0.1.0
gotchaBy default, Decomment performs strict JavaScript parsing via Esprima. If the input JavaScript or JSON code is syntactically invalid, the parser will throw an error.
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.
affects: >=0.1.0
gotchaThe `tolerant` option, which allows for less strict JavaScript parsing, was introduced in version 0.9.4. Prior versions perform strictly compliant parsing.
fix
If encountering parsing errors with non-strict JavaScript in older versions, upgrade to >=0.9.4 and use the `{ tolerant: true }` option.
affects: <0.9.4
Errors
Common errors & fixes
Error: Line X: Unexpected token
The input JavaScript or JSON code contains syntax errors or non-standard constructs that Esprima cannot parse strictly.
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 })`.
TypeError: decomment is not a function
Attempting to import `decomment` using a named import or an incorrect CommonJS `require` pattern, when it is the default/module.exports.
fix
For CommonJS, use `const decomment = require('decomment');`. For ESM, use `import decomment from 'decomment';`.
Upgrade
Version history
0.9.5latest on npm
Audit
Dependencies
esprimarequiredUsed for ECMAScript 6 compliance parsing of JavaScript and JSON content to accurately handle regular expressions and other complex syntax.
Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
decomment — npm install decomment · libregistry