Question

Cannot find module 'file-name.png' or its corresponding type declarations - typescript react

i'm trying to import a png in my typescript react project like so:
import logo from 'assets/Logo.svg';

I'm getting this TS error though:
Cannot find module 'assets/Logo.svg' or its corresponding type declarations.ts(2307)

Any idea why this is? I have attached my tsconfig.json file below in case I have configured something incorrectly:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "baseUrl": "src"
  },
  "include": ["src"]
} 
 48  44495  48
1 Jan 1970

Solution

 161

There should be a react-app-env.d.ts file in your src folder.
If you don't see it, Create a new file in src folder named react-app-env.d.ts.

Now, just add the code below in your react-app-env.d.ts file.

declare module "*.png";
declare module "*.svg";
declare module "*.jpeg";
declare module "*.jpg";

and you are good to go :)

2022-02-13

Solution

 161

There should be a react-app-env.d.ts file in your src folder.
If you don't see it, Create a new file in src folder named react-app-env.d.ts.

Now, just add the code below in your react-app-env.d.ts file.

declare module "*.png";
declare module "*.svg";
declare module "*.jpeg";
declare module "*.jpg";

and you are good to go :)

2022-02-13

Solution

 0

I created a folder in the root of project called '@types', after that i create a file called 'png.d.ts' and added the code 'declare module "*.png"'. That has solved my problem

2023-06-07

Solution

 0

I created a folder in the root of project called '@types', after that i create a file called 'png.d.ts' and added the code 'declare module "*.png"'. That has solved my problem

2023-06-07