I’m encountering an issue in my React project when trying to import an image using the following line:
import HorizontalLogo from '../assets/images/horizontal_logo.png';
The error message I’m getting is:
Cannot find module '../assets/images/horizontal_logo.png' or its corresponding type declarations.
I’ve checked the file path, and it seems to be correct. However, the error persists. Could someone guide me on resolving this import issue? Is there a specific configuration or step I might be missing? Any help would be appreciated.
Additional Context:
- The image file is located at the specified path.
- I’m using TypeScript in my React project.
- Other imports and components in the project are working fine.
Thanks in advance for any assistance!
2
Answers
try using
import HorizontalLogo from './images/horizontal_logo.png';
read this link for more info How to import image (.svg, .png ) in a React Component
To enable TypeScript to recognise and handle image file imports, you can create a declaration file in your project’s root directory. Follow these steps:
Create a file named
index.d.ts
in the root directory of your project.Inside
index.d.ts
, add the following declarations to inform TypeScript about the module type for JPG and PNG files:By doing this, TypeScript will understand that these file types are modules and allow you to import them in your code without encountering type errors.