Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

An SVG imported in Vite renders as a URL, not as a component

ErrorElement 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.

  • Applies to Vite 7, vite-plugin-svgr 4, React 19, TypeScript 5.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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)
  2. 2Match what you see, then jump to its fix

    1. 1Element type is invalid ... but got: stringThe file is imported without the ?react queryFix
    2. 2The ?react import still does not give a componentThe plugin is not installed or not in the Vite plugins listFix
    3. 3The editor or tsc reports the ?react import as an unknown moduleTypeScript does not know the ?react module typeFix
    4. 4An <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

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 | head

2Fix

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 dev

Confirmed by github.com

Did this fix it?

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 dev

Confirmed by github.com

Did this fix it?

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.ts

2Fix

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 --noEmit

Confirmed by github.com

Did this fix it?

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 | head

2Fix

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 preview

Confirmed by vite.dev

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.