SVG Sprite Command Line Interface
svg2sprite-cli provides a command-line interface for the `svg2sprite` library, designed to generate SVG sprites from a collection of individual SVG files. The current stable version is 2.0.1, though the underlying `svg2sprite` library, which `svg2sprite-cli` wraps, has not seen significant updates in approximately 9 years. The package itself has a very slow release cadence, with major version updates typically reflecting updates to its core dependency rather than new features in the CLI layer. Its key differentiator is providing a straightforward, opinionated command-line utility for SVG sprite generation, avoiding the need for direct programmatic integration of `svg2sprite` into a build system.
Common errors
-
command not found: svg2sprite
cause The `svg2sprite-cli` package was not installed globally, or the global `node_modules` bin directory is not in your system's PATH.fixInstall the package globally using `npm i -g svg2sprite-cli` or `yarn global add svg2sprite-cli`. Ensure your system's PATH includes the directory where global npm/yarn executables are installed (e.g., `/usr/local/bin` or `$HOME/.npm-global/bin`). -
Error: The source directory does not exist.
cause The provided source directory path (`<source-directory>`) either does not exist, is misspelled, or is not accessible from the current working directory.fixVerify the source directory path is correct and exists. Use an absolute path or a path relative to where you are running the `svg2sprite` command. Example: `svg2sprite ./src/icons ./dist/sprite.svg`.
Warnings
- breaking The update to `svg2sprite@2.0.0` in `svg2sprite-cli@2.0.0` indicates potential breaking changes in the underlying sprite generation logic or output format. While the CLI's command-line interface might remain compatible, the generated SVG sprite's structure, attributes, or behavior could have changed due to the major version bump of its core dependency.
- gotcha When specifying multiple attributes to remove using `--stripAttrs`, the flag must be repeated for each attribute. Providing a comma-separated list or multiple values to a single `--stripAttrs` instance will not work as expected.
- gotcha Glob patterns used with options like `--ignore` might be interpreted by the shell before being passed to `svg2sprite-cli`. This can lead to unexpected behavior if the shell expands the glob, especially with complex patterns like `**`.
Install
-
npm install svg2sprite-cli -
yarn add svg2sprite-cli -
pnpm add svg2sprite-cli
Imports
- svg2sprite
import { svg2sprite } from 'svg2sprite-cli'$ npm i -g svg2sprite-cli
- Usage
$ node svg2sprite-cli <source-directory> <dist-file>
$ svg2sprite <source-directory> <dist-file> [options]
- Options
$ svg2sprite images/svg out/sprite.svg --iconPrefix=prefix --ignore 'rainbow.svg'
Quickstart
# Install the CLI globally npm i -g svg2sprite-cli # Create a directory for your SVG icons mkdir -p src/icons # Create some dummy SVG files (replace with your actual icons) echo '<svg viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/></svg>' > src/icons/check.svg echo '<svg viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 15h2v-6h-2v6zm0-8h2V7h-2v2z"/></svg>' > src/icons/info.svg # Create an output directory mkdir -p dist # Generate the SVG sprite, ignoring one icon and adding a prefix # The output will be in dist/sprite.svg svg2sprite src/icons dist/sprite.svg --ignore 'info.svg' --iconPrefix='my-icon-' # You can verify the generated sprite content cat dist/sprite.svg # Example of how to use the sprite in HTML (conceptual): # <svg><use xlink:href="/dist/sprite.svg#my-icon-check"></use></svg>