Registry / devops / svg-minifier

svg-minifier

JSON →
library2.0.0jsnpmunverified

svg-minifier (v2.0.0) is a Node.js tool to minify SVG files by optimizing them with SVGO and optionally generating a metadata JSON and runtime script. It supports batch processing from multiple directories, callbacks for SVG content, DOM manipulation via Cheerio, and custom naming with type suffixes. Compared to standalone SVGO, it provides a higher-level batch workflow with built-in metadata output, duplicate logging, and error handling. Release cadence is irregular, maintained by cenfun.

npm install svg-minifier
INSTALL
IMPORT
SIG · SVG-MINIFIER
S
svg-minifier
devopsjavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

svgMinifier
import svgMinifier from 'svg-minifier'
const svgMinifier = require('svg-minifier')
ESM default import. The package is CommonJS but works with ESM via default import.
svgMinifier (default, named same)
const svgMinifier = require('svg-minifier')
const { svgMinifier } = require('svg-minifier')
CommonJS require returns the function directly, not an object with named exports.
svgMinifier (TypeScript)
import svgMinifier from 'svg-minifier'
import { svgMinifier } from 'svg-minifier'
When using TypeScript, default import is correct. The package does not ship types (use @types/svg-minifier if available).

Basic usage: minify all SVG files in the 'icons' directory, output metadata and runtime to 'dist'.

const path = require('path'); const svgMinifier = require('svg-minifier'); const metadata = svgMinifier({ id: 'my-icons', dirs: [path.resolve(__dirname, 'icons')], exclude: [], excludeSubDir: false, outputDir: path.resolve(__dirname, 'dist'), outputMetadata: true, outputRuntime: true, silent: false, logDuplicates: true, onFinish: (options, metadata) => { console.log('Done', metadata); } });
Debug
Known issues
breakingVersion 2.0.0: update lz-utils. May break if using internal lz-utils API directly.
fix
Check your usage of lz-utils; if relying on it directly, pin to version 1.x or update accordingly.
affects: >=2.0.0
deprecatedonSVGName, onSVGContent, onSVGDocument, onSVGOptimized, onSVGError, onFinish callbacks are stable but not typed.
fix
Refer to the options.js file for expected signatures; no types available.
affects: >=1.0.0
gotchaIf outputDir is not set, files will not be written; function still returns metadata.
fix
Always set outputDir to a valid path if you expect output files. For in-memory only, set outputMetadata: false.
affects: >=1.0.0
gotchaThe return value of onSVGOptimized can filter out the SVG: return false to exclude that icon from output. This is not documented clearly.
fix
If you want to exclude a file, return false from onSVGOptimized. Do not remove the element from Cheerio.
affects: >=1.0.0
gotchaUsing TypeScript without @types/svg-minifier may cause compilation errors.
fix
Install @types/svg-minifier (if available) or declare module 'svg-minifier' manually.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'lz-utils'
lz-utils not installed because it's a peer dependency or missing from package.json's dependencies.
fix
npm install lz-utils
svgMinifier is not a function
Using named import instead of default import (CommonJS: const { svgMinifier } = require('svg-minifier')).
fix
Use const svgMinifier = require('svg-minifier');
TypeError: Cannot read property 'path' of undefined
Missing dirs option or dirs is not an array of strings/objects.
fix
Ensure dirs is an array of directory paths or objects.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
svg-minifier — npm install svg-minifier · libregistry