Sign inDemoInstall


Package Overview
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies



Smart comment and whitespace cleaner for JavaScript-like files.

Version published
Weekly downloads
decreased by-8.56%
Install size
553 kB
Weekly downloads



[1.2.0] - 2020-09-22


  • Fix all the lint errors in src and test.
  • Emit correct typings for each bundle.
  • Revised npm scripts, using "prepare" instead "prepublish".
  • Simplify eslintrc using eslint-config-standardize + eslint-plugin-prettierx
  • Simplify tsconfig.json.
  • Sync CI configuration with new NodeJS requirements.
  • Require NodeJS 10.14.2 or 12.0 and above.
  • Update dependencies / devDependencies.




License npm Version Travis AppVeyor Coverage Code Quality Maintainability

Smart comment and whitespace cleaner for JavaScript-like files.

With js-cleanup you have:

  • Compaction of empty lines within multiline comments and/or out of them.
  • Normalization of line endings to Unix, Mac, or Windows.
  • Removal of JavaScript comments through powerful, configurable filters.
  • Removal of trailing whitespace of each line.
  • TypeScript definitions.
  • Sourcemap support.

Although js-cleanup is not locked to a particular JavaScript dialect and can handle any JS-like file: TypeScript, Flow, React, ES9, etc, it is mainly a postprocessor, so it should be runned in a later stage of your toolchain, after any preprocessor or transpiler.

js-cleanup requires node v10.14.2 or above, but avoid the buggy v11.x

Why not Uglify?

Uglify is a excelent minifier but you have little control over the results, while with js-cleanup your coding style remains intact and the removal of comments is strictly under your control.

Support my Work

I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time and effort so, if you like my work, please consider...

Of course, feedback, PRs, and stars are also welcome 🙃



$ npm install js-cleanup -D
# or
$ yarn add js-cleanup -D


jsCleanup(sourceCode: string, fileName?: string | null, options?: Options): Result;


The result is a plain JS object of this type:

type Result = {
  code: string;
  map?: object | null;
codeThe processed code.
mapA raw sourcemap object, or null if the text did not change.
Undefined if sourcemap:false.


Type definition:

type Options = {
  comments?: string | RegExp;
  compactComments?: boolean;
  maxEmptyLines?: number; // use -1 to preserve all the lines
  lineEndings?: string; // 'mac' | 'unix' | 'win'
  sourcemap?: boolean;
  sourcemapOptions: {
    includeContent?: boolean;
    inlineMap?: boolean;
    hires?: boolean;
comments'some'Filter or array of filters that determinates which comments should be preserved.
Use "all" to keep all, or "none" to remove all the comments.
compactCommentstrueShould js-cleanup also compact whitespace and blank lines in the preserved multiline comments?
Line-ending normalization is always done.
maxEmptyLines0Maximum successive empty lines to preserve in the output.
Use a positive value, or -1 to preserve all the lines
lineEndings'unix'Type of Line-ending for normalization: "unix", "mac", "win".
sourcemaptrueShould js-cleanup generate a sourcemap?
includeContentfalseInclude the original source text in the sourcemap?
inlineMapfalseInline the sourcemap in the processed text?
hirestrueShould a hi-res sourcemap be generated?

If you want to keep JSDoc comments, you should also set compactComments: false.
Since the JSDoc presentation depends on empty lines, these should be controlled by you.

Predefined Comment Filters

Instead the special 'all' or 'none' keywords, you can use any combination of custom filters along with any of these predefined ones:

NameWill preserve...
someComments containing "@license", "@preserve", or starting with "!".
licenseComments containing "@license".
eslintESLint directives.
flowFacebook Flow directives, comment types, and flowlint comments.
istanbulistanbul ignore comments.
jsdocJSDoc comments.
jshintJSHint directives.
jslintJSLint directives.
sourcesSourcemap directives sourceURL and sourceMappingURL.
tsMS TypeScript Triple-Slash and @ts-* directives, plus the @jsx pragma.
ts3sTypeScript Triple-Slash directives.

Since none of this filters is really accurate (js-cleanup is not a parser), they are suitable for the job without introducing greater complexity.

See the regexes in src/predef-filters.ts

Custom Filters

A custom filter is a regex that must match the content of the comments that you want to preserve.

The content of the comments tested by each filter does not includes the first slash, nor the */ terminator of the multiline comments, so the multiline comments begins with an asterisk (*) and single-line comments begins with a slash (/).

For example, the following filters will preserve ESLint directives and multiline comments starting with a dash:

const cleanedCode = jsCleanup(code, null, {
  comments: ["eslint", /^\*-/],


The MIT License

© 2018-2020 Alberto Martínez



Last updated on 23 Sep 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc