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 🙏

© 2026 – Pkg Stats / Ryan Hefner

image-guard

v5.2.2

Published

Near-lossless image compressor that’s easy to use and easy to automate

Readme

Image Guard

npm version Build status Socket

(This project was based on sum.cumo’s imagemin-merlin. Changes are documented, and include this README. Image Guard supports additional file formats—WebP, AVIF, and HEIC/HEIF—, comes with improved code and documentation, and is being maintained. For this reason, it’s not based on any Imagemin packages anymore.)

Image Guard takes care of near-lossless compression of your images, to help you avoid bloat in your repositories. It makes it convenient and as safe as possible to automatically compress PNG, JPG, GIF, WebP, and AVIF images. It can also convert HEIC/HEIF files to AVIF.

It’s convenient because setup is simple. Run it right away—done. Or install, run, add hook—done.

It’s as safe as possible because compression happens losslessly (near-lossless for JPG and GIF images). That allows you to stop worrying about forgetting to compress images, but also about sacrificing too much quality. (You can take care of additional optimizations manually or through other tooling.)

Installation and Use

(Note available parameters below.)

Ways to Use Image Guard

Option 1: Immediate Manual Use

You can use Image Guard right away, without installation, by running

npx image-guard

Option 2: Project-Linked Manual Use

Install Image Guard in your project:

npm i -D image-guard

Run Image Guard by calling

npx image-guard

To make sure that all images are being compressed, it’s recommended to run Image Guard like this at least once, after installation.

Option 3: Automated Use

Install Image Guard in your project:

npm i -D image-guard

To compress images already in the codebase, run Image Guard once by calling

npx image-guard

For automated use, Image Guard should be triggered through a Git hook on pre-commit. You can choose between native Git hooks (recommended for simple projects) or Husky.

Native Git Hooks

Native Git hooks are simpler to set up and don’t require additional dependencies. Run these commands from your project root:

mkdir -p .githooks;\
cat > .githooks/pre-commit << 'EOF'
#!/bin/sh
npx image-guard --staged
EOF
chmod +x .githooks/pre-commit;\
git config core.hooksPath .githooks;\
git add .githooks/pre-commit;\
git commit -m "feat: add Git pre-commit hook for Image Guard";\
npm pkg set scripts.postprepare="mkdir -p .githooks && cat > .githooks/pre-commit << 'EOF'
#\!/bin/sh
npx image-guard --staged
EOF
chmod +x .githooks/pre-commit && git config core.hooksPath .githooks"
Husky

If you already use Husky, run the following commands in your project root (you can copy and execute them at once):

grep -qxF "npx image-guard --staged" .husky/pre-commit || echo "\nnpx image-guard --staged" >> .husky/pre-commit;\
git add .husky/pre-commit;\
git commit -m "feat: add Husky pre-commit hook for Image Guard";\
npm pkg set scripts.postprepare="grep -qxF 'npx image-guard --staged' .husky/pre-commit || echo '\nnpx image-guard --staged' >> .husky/pre-commit"

If you don’t use Husky yet, run the following commands from your project root:

npm i -D husky;\
npx husky init;\
echo "npx image-guard --staged" > .husky/pre-commit;\
git add .husky/pre-commit;\
git commit -m "feat: add Husky pre-commit hook for Image Guard";\
npm pkg set scripts.postprepare="grep -qxF 'npx image-guard --staged' .husky/pre-commit || echo '\nnpx image-guard --staged' >> .husky/pre-commit"

(The postprepare script ensures that the hook is added to the repository whenever someone installs the package.)

Important: When you commit images that have not yet been compressed, the automated compression process (triggered by the pre-commit hook) will modify those image files to reduce their size. As a result, after your initial commit attempt, you will see these images appear as changed files in Git. To include the optimized images in your repository, you need to stage and commit them again. In rare cases, if further compression is possible, you may need to repeat this process until no further changes are detected. This workflow is intentional and ensures that only optimally compressed images are committed. Many editors can display diffs for images, helping you review these changes.

Parameters

  • --dry allows you to run Image Guard in “dry mode.” All changes are shown in the terminal.

  • --ignore allows you to specify paths to be ignored (as in --ignore=example,test). Multiple paths must be separated by commas. The option supports glob patterns (e.g., assets/**, **/*.png); matching is case‑insensitive and honors .gitignore.

  • --staged (recommended with automated use) triggers a mode that watches PNG, JPG, GIF, WebP, and AVIF files (and HEIC/HEIF, when --heic-to-avif is used) in git diff and only processes those files—that approach makes Image Guard more efficient in operation.

  • --heic-to-avif converts HEIC/HEIF files to AVIF. Because HEIC sources are already lossy-compressed (HEVC), the conversion uses lossy AVIF encoding (quality 80) to produce files smaller than the originals at near-identical visual quality. Original HEIC/HEIF files are deleted after conversion by default. (Note: The conversion maps Display P3 colors—common on iPhones—to sRGB, which may result in slightly less vivid colors.)

    • --keep-heic (used with --heic-to-avif) preserves the original HEIC/HEIF files instead of deleting them after conversion.
  • --quiet suppresses per‑file logs and prints only the final summary (plus errors). This reduces console noise and speeds up runs in CI and Git hooks.

Troubleshooting

“npx: command not found”

If Git hooks fail with “npx: command not found,” make sure to install (npm i -D image-guard) and to refer to the binary directly in the pre-commit hook (and, not detailed here, also in the postprepare script):

#!/bin/sh
export PATH="$PWD/node_modules/.bin:$PATH"
./node_modules/.bin/image-guard --staged

This issue can arise in GUI Git clients (VS Code, GitHub Desktop, etc.) or with Node version managers, as these environments may not inherit your shell's PATH/Node environment. This affects any tool using npx in hooks.

What Does the Output Look Like?

Roughly like this:

Screenshot of Image Guard in operation.

  • Green: The image file has been compressed.
  • Cyan: The image file has been converted (HEIC/HEIF to AVIF).
  • White (light gray): The image file has not been changed.
  • Blue: The image file had already been compressed more effectively than the new result, and was therefore skipped, too.

Tip: Use --quiet to suppress these per‑file lines and keep only the final summary.

How Does Image Guard Work?

Image Guard is a Node script that uses sharp under the hood.

Automated compression works by monitoring whether a given change list includes any PNGs, JPGs, GIFs, WebPs, or AVIFs (and HEIC/HEIF when opted in). It’s initiated by a Git hook. Only those images are compressed where there is an improvement. HEIC conversion uses heic-decode for patent-free decoding and sharp for lossy AVIF encoding (quality 80). The processed images can then be committed to the underlying repository.

Through this approach, though glossed over here, Image Guard makes up for what’s missing or complicated in other packages, namely easy, near-riskless, automatable, resource-friendly in-repo optimization.

Why Use Image Guard?

You use Image Guard when you need a simple, automatable, robust solution to compress images in a way that limits unnecessary image payload right from the start, in your repositories, and that reduces the risk that entirely uncompressed images go into production.

As Image Guard compresses near-losslessly, there’s little risk of quality issues from compression. (Lossless compression is not possible for every image format, however, so there’s a risk when excessively iterating over the same images. Doing so may eventually degrade quality.)

What Does Image Guard Not Do?

Image Guard is no substitute for image fine-tuning and micro-optimization. That’s difficult to do in an automated fashion, because this type of compression requires balancing quality and performance and is context-dependent. In its most extreme form, when maximum quality at maximum performance is required from each graphic, micro-optimization is even challenging to do manually.

That is, micro-optimization still needs to be taken care of through other means, whether manually or through tools. Image Guard just solves the problem that images are checked in or go live that are not compressed at all.

What’s Next?

There are a few ideas, like adding light SVG support, or ensuring compatibility with projects in which the project’s .git folder is not at the same level as its package.json (currently, automatic mode doesn’t work in these cases).

Feedback is appreciated: Please file an issue or send a pull request. Thank you!

License

Copyright 2019 sum.cumo GmbH Copyright 2022 Jens Oliver Meiert

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.