npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

line-commenter-tool

v2.0.0

Published

A tool to comment or uncomment lines in a file based on regex patterns and specific strings.

Readme

Line Commenter Tool

NPM Node.js Package

A developer experience utility to comment or uncomment specific lines in files (by regex or explicit strings), while preserving existing inline comments. Built with Node.js for easy integration into developer workflows (for example via Husky Git hooks).

  • Preferred usage: pair it with Husky to automatically comment/uncomment lines during post-checkout, pre-commit, or other Git hooks to streamline local development.
  • Have a success story or a use case to share? Email: [email protected]
  • If this tool helped your team, please consider sponsoring the project.

Features

  • Comment and uncomment individual lines based on regex patterns or exact string matches.
  • Support for many file types and comment styles (single-line and block comments).
  • Preserves existing inline comments when adding or removing comment wrappers.
  • Handles nested comments by adding or removing one layer of commenting at a time.
  • Command-line interface for easy automation and integration.

Supported comment styles

The tool works with common single-line and block comment styles including, but not limited to:

  • Single-line: // (JS/TS), # (sh, py), -- (SQL)
  • Block comments: /* ... */ (CSS/JS), <!-- ... --> (HTML) Note: behavior varies with language; use --multiline to operate on block comments.

Installation

Install globally:

npm install -g line-commenter-tool

Or use without installing via npx:

npx line-commenter-tool --help

Basic Usage

CLI pattern:

line-commenter-tool <action> <filename> <regexPattern> [string1,string2,...] [options]
  • <action>: comment or uncomment
  • <filename>: path to the file to process (relative or absolute)
  • <regexPattern>: regex pattern (as a quoted string) used to match lines
  • [string1,string2,...] (optional): comma-separated exact strings to match

Options:

  • --help Show help and exit
  • --version Show version and exit
  • --silent Suppress output messages
  • --multiline Enable processing of multiline block comments (e.g., /* ... */, <!-- ... -->)

Behavior notes:

  • The tool is case-sensitive by default. Ensure your regex or string matches the target casing.
  • The tool modifies files in place. Use version control or back up files before running on important files.

Examples

  1. Comment all lines containing console.log in a JS file:
line-commenter-tool comment app.js "console\\.log"
  1. Uncomment all lines that contain TODO in a Python file:
line-commenter-tool uncomment script.py "TODO"
  1. Comment lines matching multiple explicit strings (comma-separated):
line-commenter-tool comment config.yml "DEBUG" "error,warning"
  1. Uncomment a full multiline block in a CSS file:
line-commenter-tool uncomment styles.css "/\\*" --multiline
  1. Use with npx (no install):
npx line-commenter-tool comment src/index.js "debug\\(" --silent

Tips:

  • When writing shell commands in examples, escape regex metacharacters appropriately (see examples above).
  • If a pattern contains spaces or shell-special characters, wrap it in single quotes.

Integration with Husky

Husky can run this tool as part of Git hooks to automatically apply comment/uncomment transformations.

Install Husky:

npm install husky --save-dev
npx husky install

Add a pre-commit hook that runs the tool:

npx husky add .husky/pre-commit "npx line-commenter-tool comment path/to/file.js 'console\\.log\\(\\)' 'TODO'"
chmod +x .husky/pre-commit

Example .husky/pre-commit:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# Comment out console.log and TODO lines before commit
npx line-commenter-tool comment path/to/file.js 'console\.log\(' 'TODO'

Safety & Best Practices

  • The tool edits files in place. Keep files under version control so changes can be reviewed and reverted.
  • Test your regex patterns on small files or with controlled inputs before running across a codebase.
  • Consider running the command locally with --silent disabled to confirm behavior before automating in hooks.

Contribution

Contributions and improvements are welcome. Please open an issue or submit a pull request.

If you'd like me to prepare a branch and PR with this README update, tell me the target branch name and I can create it for you.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Support

If you encounter issues or have questions, please open an issue on the GitHub repository: https://github.com/darkmastermindz/line-commenter-tool/issues