Correct path for img on React.js
Solution 1
You're using a relative url, which is relative to the current url, not the file system. You could resolve this by using absolute urls
<img src ="http://localhost:3000/details/img/myImage.png" />
But that's not great for when you deploy to www.my-domain.bike, or any other site. Better would be to use a url relative to the root directory of the site
<img src="/details/img/myImage.png" />
Solution 2
In create-react-app
relative paths for images don't seem to work. Instead, you can import an image:
import logo from './logo.png' // relative path to image
class Nav extends Component {
render() {
return (
<img src={logo} alt={"logo"}/>
)
}
}
Solution 3
If you used create-react-app to create your project then your public folder is accessible. So you need to add your image
folder inside the public folder.
public/images/
<img src="/images/logo.png" />
Solution 4
With create-react-app
there is public folder (with index.html...).
If you place your "myImage.png" there, say under img sub-folder, then you can access them through:
<img src={window.location.origin + '/img/myImage.png'} />
Solution 5
If the image is placed inside the 'src' folder, use the following:
<img src={require('../logo.png')} alt="logo" className="brand-logo"/>
Related videos on Youtube
Comments
-
onedkr almost 2 years
I have some problem with my images on my react project. Indeed I always thought that relative path into src attribute was built on the files architecture
Here my files architecture:
components file1.jsx file2.jsx file3.jsx container img js ...
However I realized that the path is built on the url. In one of my component (for example into file1.jsx) I have this:
localhost/details/2 <img src="../img/myImage.png" /> -> works localhost/details/2/id <img src="../img/myImage.png" /> -> doesn't work, images are not displayed
How is it possible to solve this problem? I want that in any form of routes handled by react-router, all images can be displayed with the same path.
-
omarjmh almost 8 yearsjust point directly to the image, dont use ../ whatever
-
today about 6 yearsYou need to use
require
. Read this answer on SO for more info. -
Mokesh S almost 4 yearsHope this answer helps you out React local images
-
-
user1322092 over 7 yearsThanks, I would emphasize the
/
that precedesdetails
(vs./details
, etc, for others who may conflate the two). -
zok almost 7 yearswhat about dynamic images, for instance, referenced in a json file, in create-react-app?
-
claireablani almost 7 years@zok I think you would export the variable from the JSON file, import the variable into your component. Then you can reference it as usual. e.g. export const my_src = variable_here, import {my_src} from my_file, and src={my_src}.
-
Hitendra almost 7 years
import './styles/style.less'; **import logo from './styles/images/deadline.png';**
I am getting error on 2nd line module not found while image exists & path is correct. -
Andre about 6 yearsThis method is also confirmed by by survive.js if you want to take a look at one of the core webpack author's methods. He also mentions you can use babel-plugin-transform-react-jsx-img-import to get around having to import the image every time you reference an image.
-
Sami about 6 yearsNot this, it solves only image issue but does not solve the path issue.. Dima Gershman' answer is the actual react solution for path of any file img or else
-
Sami about 6 yearsThis should be marked as the correct answer, because it is actually reactjs solution for all types of paths of any files
-
Jimit Patel almost 6 yearsEven for me it's not working. I am using
reactjs
withcordova
and usingES5
as eslint -
Maderas over 5 yearsI don't understand why you think this is the correct answer. I just tried this answer and Claireblani's answer and claire's answer worked while this did not. This answer only works if the image is in the public folder. Should my images go in the public folder? @Sami
-
Sami over 5 yearsYes @Maderas first things is is this answer is same as the accepted one => just removing {} and base url you can use simply src='/relative path of image' or src='absolute path of image' Whats more in this answer is that it accommodates variable path for any image
-
Raphael over 5 yearsthis helped me :)
-
Athir Nuaimi over 5 yearsWhile both this and claireablani's solution work, claireablani's is what the create-react-app docs recommend. They list a number of benefits over putting the resources in public facebook.github.io/create-react-app/docs/…
-
Jeremy Rea about 5 yearsThis won't work, a slash ( / ) is required at the start to indicate relative path, like so: "/images/pitbull-mark.png"
-
Mustkeem K about 5 yearshave you tried the one mentioned in the answer? This should work too.
-
Nuhman almost 5 years@user1322092 is right. Remember it's
/images/popcorn.png
and not./images/popcorn.png
. Worked for me with all the routes and stuff. -
Si8 almost 5 yearsI was about to ask a question but THIS answer solved my issue!!! Should be marked as answer. Thanks!!! +1
-
awasik over 4 yearsIt worked for me. Just remember that you need to restart the app if you added new image to the public directory.
-
AndriyFM about 4 yearsThis is the best practice for me who used for dummy image which located in the same root folder
-
Maria Campbell about 4 yearsSo far works for me like a charm. Nothing else did! And I tried just about everything. Thanks!
-
Alexandru Pirvu almost 4 yearsIt is indeed a fast way to treat the situations, but it will work only if you don't need relative URLs. E.g. if you serve your app at
www.example.com/react-app
, the public folder will be exposed on www.example.com, withoutreact-app
. This can be troublesome, so the accepted answer is the correct one. Sources: adding assets and using public folder -
Mr_LinDowsMac almost 4 yearsThat worked for me. This works in the case you DON'T want to store images in public folder
-
Piyush N over 3 yearsIn my case, Even json file is dynamic and logo can we fetched from either A or B file depends on condition. How can solve this case with your approach? FYI: I don't want to use multiple import because we have many client lets say 1000, in that case if we go with this approach we will end up using import statement 1000 times! for using logo conditionally.
-
Soon Santos over 3 yearsCheck create-react-app/adding-images.. documentation for more.
-
kojow7 over 3 years@JeremyRea A slash at the start indicates an absolute path, not a relative path.
-
mercury almost 3 yearsthe / will follow the component url, not work
-
rainversion_3 almost 3 yearslike @Dima mentioned below, if you used
create-react-app
then you can place the images inpublic->myImgFolder->myImg.png
folder and use it as<img src="/myImgFolder/myImg.png" alt={"logo"}/>
-
nimsrules about 2 yearsFor anyone facing [object Module] error in the img's src, do this
src={require('../logo.png').default}
-
Rohit Kumar Shrivastava almost 2 yearsThis works for
.csv
files as well. A great help.