An SVG imported in Vite renders as a URL, not as a component
Element type is invalid: expected a string (for built-in components) or a class/function but got: string.Short answer
Vite returns a plain SVG import as a URL string. To get a React component, install vite-plugin-svgr, add svgr() to the plugins in your Vite config, and import the file with the ?react query. In TypeScript, add the plugin's client types so the import is accepted.
Fix it fast
1Find out which cause you have
Log what the import actually returns. A path string means the plugin is not in play for that import.
// temporary line in the file that imports the SVG import Logo from './logo.svg?react' console.log(typeof Logo)2Match what you see, then jump to its fix
- 1
Element type is invalid ... but got: stringThe file is imported without the ?react queryFix - 2
The ?react import still does not give a componentThe plugin is not installed or not in the Vite plugins listFix - 3
The editor or tsc reports the ?react import as an unknown moduleTypeScript does not know the ?react module typeFix - 4
An <img src> or CSS background works with the plain import and breaks after adding the pluginYou only want the file's URL, not a componentFix
- 1
What does the error look like?
Element type is invalid: expected a string (for built-in components) or a class/function but got: string.An imported .svg is a string (a URL path) instead of a component
Cause 1 of 4
The file is imported without the ?react query
1Check
A plain import of a static asset returns its public URL, so <Logo /> receives a string.
grep -rn "\.svg'" src | head2Fix
Add ?react to the import. Use the same import for every SVG you want as a component.
import Logo from './logo.svg?react'
export function Header() {
return <Logo width={32} height={32} />
}3Verify
Start the dev server and check that the SVG draws inline in the DOM.
npm run devConfirmed by github.com
Cause 2 of 4
The plugin is not installed or not in the Vite plugins list
1Check
Check that the package is in devDependencies and that svgr() is called inside the plugins array.
grep -n "svgr" package.json vite.config.*2Fix
Install the plugin and register it.
npm install --save-dev vite-plugin-svgr
// vite.config.ts
import svgr from 'vite-plugin-svgr'
export default {
plugins: [svgr()],
}3Verify
Restart the dev server and check the import again.
npm run devConfirmed by github.com
Cause 3 of 4
TypeScript does not know the ?react module type
1Check
The build may work while the editor and tsc complain, because no declaration covers the ?react import.
cat src/vite-env.d.ts2Fix
Reference the plugin's client types in the Vite environment declaration file.
// src/vite-env.d.ts
/// <reference types="vite-plugin-svgr/client" />3Verify
Run the type check.
npx tsc --noEmitConfirmed by github.com
Cause 4 of 4
You only want the file's URL, not a component
1Check
Plain imports are meant for URLs, and the ?url query asks for the URL on purpose.
grep -rn "\.svg" src | head2Fix
Keep the plain or ?url import for <img> usage, and use ?react only where you need a component.
import logoUrl from './logo.svg?url'
// <img src={logoUrl} alt="Logo" />3Verify
Load the page and check that the image appears.
npm run build && npm run previewConfirmed by vite.dev
How do I stop it coming back?
- Pick one convention per project: ?react for components, plain or ?url for images.
- Commit the types reference in vite-env.d.ts so a fresh clone type checks.
- Restart the dev server after changing the plugins list.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix