Importing an SVG file in Next.js fails or shows nothing
Can't import SVG into Next.jsShort 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.
Fix it fast
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.*2Match what you see, then jump to its fix
- 1
<Logo /> from import Logo from './logo.svg' failsYou want the SVG as a React component, and no loader is configuredFix - 2
The SVG is used only as a pictureYou want a plain image file, not a componentFix - 3
A broken image for an SVG whose src does not end in .svgA remote or extension-less SVG url goes through the optimizerFix
- 1
What does the error look like?
Can't import SVG into Next.jsimport 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 devConfirmed by nextjs.org
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/*.svg2Fix
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 devConfirmed by nextjs.org
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 devConfirmed by nextjs.org
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
Web apps
TypeScript cannot find module './logo.png' or its type declarations
TS2307: Cannot find module '../assets/hero.svg' or its corresponding type declarations.
The bundler can load an image, but TypeScript knows nothing about files that are not code.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fix