Introduction: "Cannot find module" — a familiar pain?
\n\nYou are writing in TypeScript, add a beautiful SVG icon to your project, and try to import it in the standard way:
\n\nimport Logo from './assets/logo.svg';\n\nAnd then TypeScript gives you a red error:
\n\nCannot find module './assets/logo.svg' or its corresponding type declarations.\n\nFamiliar? If you have ever encountered this problem — you are not alone. The question "Unable to import svg files in typescript" on Stack Overflow has gathered thousands of views and dozens of answers. The problem is that TypeScript by default does not know how to handle non-code files (SVG, PNG, CSS).
\n\nIn this article, we will break down a complete explanation with code: why the error occurs and how to fix it once and for all. You will learn about declaration files, configuring Webpack and Vite, and how to properly type SVG in React projects.
\n\nWhy doesn't TypeScript see SVG?
\n\nTypeScript is a strictly typed language. It checks types at compile time. When you write import Logo from './logo.svg', TypeScript expects the module ./logo.svg to export something with a known type. But SVG is not a TypeScript file; it is XML. TypeScript has no built-in description for SVG modules.
Hence the error: the compiler cannot find type declarations for this file.
\n\nThere are three main ways to solve the problem:
\n\n- \n
- Method 1: Create a global declaration file (
.d.ts) \n - Method 2: Use Webpack/Vite with the correct loaders \n
- Method 3: Import SVG via
urlorraw(for specific cases) \n
We will cover each method with code examples.
\n\nMethod 1: Global declaration file (most popular)
\n\nThis is the Stack Overflow solution that works in 90% of cases. You create a file with the extension .d.ts (e.g., src/declarations.d.ts or src/svg.d.ts) and declare a module for SVG.
Here is the minimal code:
\n\n// src/declarations.d.ts\ndeclare module '*.svg' {\n const content: string;\n export default content;\n}\n\nWhat is happening here? We are telling TypeScript: "Any file with the extension .svg is a module that exports a string by default (the file path or content)."
If you are using React and want to import SVG as a React component (via @svgr/webpack or vite-plugin-svgr), the declaration will be different:
// src/declarations.d.ts\ndeclare module '*.svg' {\n import React from 'react';\n const SVGComponent: React.FC<React.SVGProps<SVGSVGElement>>;\n export default SVGComponent;\n}\n\nImportant: Make sure the .d.ts file is included in your tsconfig.json. Usually, it is enough if it is inside src/, but if not — add the path to the include field:
// tsconfig.json\n{\n "compilerOptions": {\n // ... other settings\n },\n "include": ["src/**/*"]\n}\n\nAfter this, the "Cannot find module" error will disappear, and you will be able to import SVG as usual.
\n\nMethod 2: Configuring Webpack or Vite
\n\nThe global declaration is only half the battle. To make the import actually work at runtime (in the browser), you need to configure the bundler.
\n\nFor Webpack
\n\nInstall file-loader or url-loader:
npm install --save-dev file-loader\n\nAdd a rule to webpack.config.js:
// webpack.config.