Strip line and/or block comments from a string. Blazing fast, and works with JavaScript, Sass, CSS, Less.js, and a number of other languages.
npx @tessl/cli install tessl/npm-strip-comments@2.0.0Strip Comments is a high-performance utility library for removing line and/or block comments from source code strings. It supports multiple programming languages including JavaScript, CSS, Sass, Less.js, Python, C, Java, and many others, with flexible configuration options for selective comment removal and newline preservation.
npm install strip-commentsconst strip = require("strip-comments");For ES modules:
import strip from "strip-comments";const strip = require("strip-comments");
const code = `
// This is a line comment
const foo = "bar"; /* block comment */
/*!
* Protected comment
*/
console.log(foo);
`;
// Remove all comments
const clean = strip(code);
console.log(clean);
// => const foo = "bar"; console.log(foo);
// Remove only block comments
const lineOnly = strip.block(code);
console.log(lineOnly);
// => // This is a line comment const foo = "bar"; console.log(foo);
// Remove only line comments
const blockOnly = strip.line(code);
console.log(blockOnly);
// => const foo = "bar"; /* block comment */ /*! * Protected comment */ console.log(foo);Remove all line and block comments from a string, with options to preserve protected comments and newlines.
/**
* Strip all code comments from the given input, including protected
* comments that start with `!`, unless disabled by setting `options.keepProtected`
* to true.
* @param {string} input - string from which to strip comments
* @param {object} [options] - optional options
* @param {boolean} [options.line=true] - if `false` strip only block comments
* @param {boolean} [options.block=true] - if `false` strip only line comments
* @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `/*!` and `//!`)
* @param {boolean} [options.safe=false] - Alias for keepProtected
* @param {boolean} [options.preserveNewlines=false] - Preserve newlines after comments are stripped
* @returns {string} modified input
*/
function strip(input, options);Usage Examples:
const strip = require("strip-comments");
// Basic usage - remove all comments
const result = strip('const foo = "bar";// this is a comment\n /* me too */');
console.log(result);
// => 'const foo = "bar";\n '
// Keep protected comments (using keepProtected)
const protected = strip('const foo = "bar";/*! important *//* regular */', {
keepProtected: true
});
console.log(protected);
// => 'const foo = "bar";/*! important */'
// Keep protected comments (using safe alias)
const protectedSafe = strip('const foo = "bar";/*! important *//* regular */', {
safe: true
});
console.log(protectedSafe);
// => 'const foo = "bar";/*! important */'
// Preserve newlines
const withNewlines = strip('// comment\nconst foo = "bar";', {
preserveNewlines: true
});
console.log(withNewlines);
// => '\nconst foo = "bar";'Remove only block comments while preserving line comments.
/**
* Strip only block comments.
* @param {string} input - string from which to strip comments
* @param {object} [options] - optional options
* @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `/*!`)
* @param {boolean} [options.safe=false] - Alias for keepProtected
* @returns {string} modified string
*/
strip.block(input, options);Usage Examples:
const strip = require("strip-comments");
const code = 'const foo = "bar";// this is a comment\n /* me too */';
const result = strip.block(code);
console.log(result);
// => 'const foo = "bar";// this is a comment\n 'Remove only line comments while preserving block comments.
/**
* Strip only line comments.
* @param {string} input - string from which to strip comments
* @param {object} [options] - optional options
* @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `//!`)
* @param {boolean} [options.safe=false] - Alias for keepProtected
* @returns {string} modified string
*/
strip.line(input, options);Usage Examples:
const strip = require("strip-comments");
const code = 'const foo = "bar";// this is a comment\n /* me too */';
const result = strip.line(code);
console.log(result);
// => 'const foo = "bar";\n/* me too */'Strip the first comment from the given input. Or, if keepProtected is true, the first non-protected comment will be stripped.
/**
* Strip the first comment from the given input. Or, if `opts.keepProtected` is true,
* the first non-protected comment will be stripped.
* @param {string} input - string from which to strip comments
* @param {object} [options] - optional options
* @param {boolean} [options.keepProtected=false] - Keep comments with `!`
* @param {boolean} [options.safe=false] - Alias for keepProtected
* @returns {string} modified string
*/
strip.first(input, options);Usage Examples:
const strip = require("strip-comments");
const code = '//! first comment\n// second comment\nconst foo = "bar";';
const result = strip.first(code, { keepProtected: true });
console.log(result);
// => '//! first comment\nconst foo = "bar";'Parse a string and return a basic CST (Concrete Syntax Tree) for advanced processing.
/**
* Parses a string and returns a basic CST (Concrete Syntax Tree).
* @param {string} input - string to parse
* @param {object} [options] - optional options
* @param {string} [options.language='javascript'] - Language name for comment syntax
* @param {boolean} [options.block=true] - Parse block comments
* @param {boolean} [options.line=true] - Parse line comments
* @param {boolean} [options.first=false] - Parse only first comment
* @returns {Block} parsed syntax tree
*/
strip.parse(input, options);Usage Examples:
const strip = require("strip-comments");
const code = 'const foo = "bar"; // comment';
const cst = strip.parse(code);
console.log(cst);
// => Block { type: 'root', nodes: [...] }Strip Comments supports comment removal for the following languages through the language option:
// line comments, /* */ block comments/* */ block comments only// line comments, /* */ block comments# and // line comments, /* */ block comments# line comments, """ block comments# line comments, =begin/=end block comments# line comments-- line comments, --[[/]] block comments<!-- --> block comments-- line comments, {-/-} block comments-- line comments% line comments, %{/%} block comments(* / *) block comments⍝ line comments#! line commentsUsage with Different Languages:
const strip = require("strip-comments");
// Python code
const pythonCode = '# This is a comment\nprint("Hello")';
const cleanPython = strip(pythonCode, { language: 'python' });
// CSS code
const cssCode = '/* Comment */ body { color: red; }';
const cleanCSS = strip(cssCode, { language: 'css' });
// HTML code
const htmlCode = '<!-- Comment --> <div>Hello</div>';
const cleanHTML = strip(htmlCode, { language: 'html' });The library throws specific errors for invalid inputs:
const strip = require("strip-comments");
try {
strip(123); // TypeError: Expected input to be a string
} catch (error) {
console.error(error.message);
}
try {
strip(code, { language: 'invalid' }); // Error: Language "invalid" is not supported
} catch (error) {
console.error(error.message);
}/**
* Options object for strip functions
*/
interface StripOptions {
/** if `false` strip only block comments, default `true` */
line?: boolean;
/** if `false` strip only line comments, default `true` */
block?: boolean;
/** Keep ignored comments (e.g. `/*!` and `//!`) */
keepProtected?: boolean;
/** Alias for keepProtected */
safe?: boolean;
/** Preserve newlines after comments are stripped */
preserveNewlines?: boolean;
/** Language name for comment syntax (default: 'javascript') */
language?: string;
/** Parse only first comment (for parse function) */
first?: boolean;
}
/**
* Node in the concrete syntax tree
*/
class Node {
type: string;
value?: string;
match?: RegExpMatchArray;
newline: string;
/** Whether this is a protected comment (starts with !) */
readonly protected: boolean;
}
/**
* Block node that can contain child nodes
*/
class Block extends Node {
nodes: Node[];
push(node: Node): void;
/** Whether this block contains a protected comment */
readonly protected: boolean;
}