Maaz Zindani

Loading the index…

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

Importing an SVG file in Next.js fails or shows nothing

ErrorCan't import SVG into Next.js

Short answer

Next.js treats an imported .svg as an image file, not as a React component. To show it as a picture, use next/image. To use it as a component you can style, add the @svgr/webpack loader under the turbopack rules in next.config. Remote SVGs through the optimizer are rejected by default.

  • Applies to Next.js 16, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Decide how you want to use it: as a picture (img) or as inline SVG you can style. The two use different fixes below.

    cat next.config.*
  2. 2Match what you see, then jump to its fix

    1. 1<Logo /> from import Logo from './logo.svg' failsYou want the SVG as a React component, and no loader is configuredFix
    2. 2The SVG is used only as a pictureYou want a plain image file, not a componentFix
    3. 3A broken image for an SVG whose src does not end in .svgA remote or extension-less SVG url goes through the optimizerFix

What does the error look like?

  • Can't import SVG into Next.js
  • import Logo from './logo.svg' used as <Logo /> does not render as a component

Cause 1 of 3

You want the SVG as a React component, and no loader is configured

1Check

Check next.config for a turbopack.rules entry for *.svg.

grep -n "svg" next.config.*

2Fix

Install SVGR and map *.svg to it. The Next.js docs use this exact example.

npm install -D @svgr/webpack

// next.config.js
module.exports = {
  turbopack: {
    rules: {
      '*.svg': {
        loaders: ['@svgr/webpack'],
        as: '*.js',
      },
    },
  },
}

3Verify

Restart the dev server and render the imported component.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

You want a plain image file, not a component

1Check

Put the file in the public folder and reference it by path with next/image.

ls public/*.svg

2Fix

Use the path as src. Next.js serves SVG as-is, without optimization, when src ends in .svg.

import Image from 'next/image'

export default function Header() {
  return <Image src="/logo.svg" alt="Logo" width={120} height={40} />
}

3Verify

The image renders on the page.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

A remote or extension-less SVG url goes through the optimizer

1Check

The optimizer rejects SVG by default (dangerouslyAllowSVG is false).

grep -n "dangerouslyAllowSVG" next.config.*

2Fix

Pass unoptimized on that image. Only if you cannot, enable dangerouslyAllowSVG together with a strict contentSecurityPolicy.

<Image src="https://cdn.example.com/logo" alt="Logo" width={120} height={40} unoptimized />

3Verify

Reload; the SVG renders directly from its source.

npm run dev

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Pick one SVG strategy per project and document it in next.config.
  • Restart the dev server after changing next.config.

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

Next step

Want this handled for good?

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