0

None of the images I have saved locally can be found when trying to import them in my project.

import {portfolio} from './portfolio.png'

Leads to "Cannot find module './portfolio.png' or its corresponding type declarations.ts(2307)".

The image file path is 100% correct.

Update: Image loads however I would like to know how to remove the typescript error.

3 Answers 3

1

Try default import: import portfolio from './portfolio.png'.

Sign up to request clarification or add additional context in comments.

1 Comment

no this doesn't work either. It can't find the image file at all. Not sure why?
1

Use the ES6 standard to import images into react.

If in public use the absolute path.

If it is in src you can use relative path.

https://create-react-app.dev/docs/adding-images-fonts-and-files/

import MyImage from './logo.svg'; # In local
import MyImage from 'images/logo.svg'; # In Public

export default function App() {
   return(
     <div>
       <img src={MyImage} alt="logo" />
     </div>
   );
}

1 Comment

I'm using es6 method I think its a typescript error
0

Hey @Joe there are multiple ways to call images- first-

import image from './porfolio.png'

second, in this method you need to post image inside public images folder-

<img src="/images/image.png"/>

Hope these methods help you to solve your query, if you still facing issue just lemme know, I will hlep you. Thanks

7 Comments

The image renders but I'm getting an annoying ts error.
What kind of error? Can you please share screenshot by link
"Cannot find module './portfolio.png' or its corresponding type declarations.ts(2307)"
Did you really have portfolio.png in the system folder? Also check the name of the file
Yes it's all correct because it works when I use require(). There's just a type error when I import.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.