Registry / web-framework / vite-plugin-svgr

vite-plugin-svgr

JSON →
library5.2.0jsnpmunverified

Vite plugin to transform SVG files into React components using SVGR. Current stable version is 5.2.0, with regular updates. Key differentiator: seamless integration with Vite, supports both import with ?react query and default export, optional SVGO optimization, and TypeScript declarations. Compared to alternatives like @svgr/webpack, it is Vite-native and simplifies SVG component usage in React projects.

npm install vite-plugin-svgr
INSTALL
IMPORT
SIG · VITE-PLUGIN-SVGR
V
vite-plugin-svgr
web-frameworkjavascriptv5.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

svgr
import svgr from 'vite-plugin-svgr'
const svgr = require('vite-plugin-svgr')
Default import is the standard; CommonJS require may not work with ESM-only configuration.
default export with ?react
import Logo from './logo.svg?react'
import Logo from './logo.svg'
The ?react query is required in v4+ to trigger SVG transformation. Without it, the file is treated as a regular asset URL.
TypeScript declaration
/// <reference types="vite-plugin-svgr/client" />
/// <reference types="vite-plugin-svgr" />
The correct path is 'vite-plugin-svgr/client', not just 'vite-plugin-svgr'.
default export (SVG source)
import svgUrl from './logo.svg'
Importing without ?react gives the SVG file URL string, not a React component.

Demonstrates how to set up vite-plugin-svgr in a Vite project with React, including import syntax and TypeScript declaration.

// vite.config.ts import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import svgr from 'vite-plugin-svgr'; export default defineConfig({ plugins: [ react(), svgr({ svgrOptions: { icon: true, expandProps: 'end', }, include: '**/*.svg?react', }), ], }); // App.tsx import ReactLogo from './logo.svg?react'; function App() { return ( <div> <ReactLogo width={100} height={100} /> </div> ); } export default App; // vite-env.d.ts /// <reference types="vite-plugin-svgr/client" />
Debug
Known issues
breakingIn v4.0.0, SVG import behavior changed: you must append ?react query to import as React component. Without it, you get the default export (SVG URL).
fix
Use import Logo from './logo.svg?react' instead of import Logo from './logo.svg'.
affects: >=4.0.0
breakingv5.0.0 dropped Vite 2 support. Minimum Vite version is now 3.0.0.
fix
Upgrade Vite to v3+.
affects: >=5.0.0
deprecatedv4.0.0 changed the default export to the SVG file URL (string). If you relied on importing SVG as a component without ?react, it will break.
fix
Add ?react query to import React component.
affects: >=4.0.0 <5.0.0
gotchaSVGO is not enabled by default. If you expect SVGO optimization, you need to manually install @svgr/plugin-svgo and configure it via svgrOptions.
fix
Install @svgr/plugin-svgo and add to svgrOptions.plugins.
affects: >=3.0.0
gotchaTypeScript declaration must be referenced with 'vite-plugin-svgr/client' not 'vite-plugin-svgr'.
fix
Add /// <reference types="vite-plugin-svgr/client" /> to your vite-env.d.ts.
affects: >=3.0.0
breakingv3.2.0 re-added CJS entry, but since v4.0.0 the package is ESM-only. CommonJS require will fail.
fix
Use ESM imports (import) or set type: 'module' in package.json.
affects: >=4.0.0
Errors
Common errors & fixes
Uncaught Error: @svgr/core tried to access @svgr/plugin-svgo but it's not installed
SVGO plugin not installed but configured in svgrOptions.plugins.
fix
Install @svgr/plugin-svgo as a dependency.
Module not found: Error: Can't resolve 'vite-plugin-svgr'
Package not installed or mismatched Vite version.
fix
Run npm install -D vite-plugin-svgr and ensure Vite >=3 is installed.
Cannot find module 'vite-plugin-svgr' or its corresponding type declarations.
Missing TypeScript declaration for SVG imports.
fix
Add /// <reference types="vite-plugin-svgr/client" /> in a .d.ts file.
The requested module 'vite-plugin-svgr' does not provide an export named 'default'
Using CommonJS require in a project that expects ESM import.
fix
Use import svgr from 'vite-plugin-svgr' instead of require()
Upgrade
Version history
5.2.0latest on npm
Audit
Dependencies
viterequiredPeer dependency; plugin designed for Vite build tool
@svgr/corerequiredCore SVGR transformation library (lazy loaded since v5.1.0)
@svgr/plugin-svgooptionalOptional plugin for SVGO optimization if configured
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
vite-plugin-svgr — npm install vite-plugin-svgr · libregistry