Problem with relative path when deploying react app to a subfolder in GitHub pages









up vote
0
down vote

favorite












I have a react app with this project structure:



enter image description here



In my Home component and About component I have two images
With src set to :
<img src="/img/charlie1.jpg" alt="charlie home pic" />
and <img src="/img/charlie2.jpg" alt="charlie about pic" />



So I assume it will start looking in the root folder then the image folder then find the pictures in there.



This works fine when I’m developing on my local machine
The images will display correctly.



But when I build it and move the files to prod the images won’t work
It looks for the image at this path: https://xyz.github.io/img/charlie2.jpg instead of https://xyz.github.io/charlieReact/img/charlie2.jpg



How can I fix this problem?



If I change the image src to : <img src="./img/charlie1.jpg" alt="charlie home pic" />



Then it works in github pages, but then the images won’t display when I’m on my local machine…










share|improve this question





















  • May I ask why you don't want to import the images in the JavaScript, i.e. import charlie1 from './charlie1.jpg'? Do you have too many images? Maybe this could work.
    – Tholle
    Nov 9 at 22:01











  • Ah! that worked. Is that how it should be done?
    – psj01
    Nov 9 at 22:08






  • 1




    It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves. If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.
    – Tholle
    Nov 9 at 22:12














up vote
0
down vote

favorite












I have a react app with this project structure:



enter image description here



In my Home component and About component I have two images
With src set to :
<img src="/img/charlie1.jpg" alt="charlie home pic" />
and <img src="/img/charlie2.jpg" alt="charlie about pic" />



So I assume it will start looking in the root folder then the image folder then find the pictures in there.



This works fine when I’m developing on my local machine
The images will display correctly.



But when I build it and move the files to prod the images won’t work
It looks for the image at this path: https://xyz.github.io/img/charlie2.jpg instead of https://xyz.github.io/charlieReact/img/charlie2.jpg



How can I fix this problem?



If I change the image src to : <img src="./img/charlie1.jpg" alt="charlie home pic" />



Then it works in github pages, but then the images won’t display when I’m on my local machine…










share|improve this question





















  • May I ask why you don't want to import the images in the JavaScript, i.e. import charlie1 from './charlie1.jpg'? Do you have too many images? Maybe this could work.
    – Tholle
    Nov 9 at 22:01











  • Ah! that worked. Is that how it should be done?
    – psj01
    Nov 9 at 22:08






  • 1




    It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves. If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.
    – Tholle
    Nov 9 at 22:12












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a react app with this project structure:



enter image description here



In my Home component and About component I have two images
With src set to :
<img src="/img/charlie1.jpg" alt="charlie home pic" />
and <img src="/img/charlie2.jpg" alt="charlie about pic" />



So I assume it will start looking in the root folder then the image folder then find the pictures in there.



This works fine when I’m developing on my local machine
The images will display correctly.



But when I build it and move the files to prod the images won’t work
It looks for the image at this path: https://xyz.github.io/img/charlie2.jpg instead of https://xyz.github.io/charlieReact/img/charlie2.jpg



How can I fix this problem?



If I change the image src to : <img src="./img/charlie1.jpg" alt="charlie home pic" />



Then it works in github pages, but then the images won’t display when I’m on my local machine…










share|improve this question













I have a react app with this project structure:



enter image description here



In my Home component and About component I have two images
With src set to :
<img src="/img/charlie1.jpg" alt="charlie home pic" />
and <img src="/img/charlie2.jpg" alt="charlie about pic" />



So I assume it will start looking in the root folder then the image folder then find the pictures in there.



This works fine when I’m developing on my local machine
The images will display correctly.



But when I build it and move the files to prod the images won’t work
It looks for the image at this path: https://xyz.github.io/img/charlie2.jpg instead of https://xyz.github.io/charlieReact/img/charlie2.jpg



How can I fix this problem?



If I change the image src to : <img src="./img/charlie1.jpg" alt="charlie home pic" />



Then it works in github pages, but then the images won’t display when I’m on my local machine…







reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 21:49









psj01

9821225




9821225











  • May I ask why you don't want to import the images in the JavaScript, i.e. import charlie1 from './charlie1.jpg'? Do you have too many images? Maybe this could work.
    – Tholle
    Nov 9 at 22:01











  • Ah! that worked. Is that how it should be done?
    – psj01
    Nov 9 at 22:08






  • 1




    It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves. If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.
    – Tholle
    Nov 9 at 22:12
















  • May I ask why you don't want to import the images in the JavaScript, i.e. import charlie1 from './charlie1.jpg'? Do you have too many images? Maybe this could work.
    – Tholle
    Nov 9 at 22:01











  • Ah! that worked. Is that how it should be done?
    – psj01
    Nov 9 at 22:08






  • 1




    It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves. If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.
    – Tholle
    Nov 9 at 22:12















May I ask why you don't want to import the images in the JavaScript, i.e. import charlie1 from './charlie1.jpg'? Do you have too many images? Maybe this could work.
– Tholle
Nov 9 at 22:01





May I ask why you don't want to import the images in the JavaScript, i.e. import charlie1 from './charlie1.jpg'? Do you have too many images? Maybe this could work.
– Tholle
Nov 9 at 22:01













Ah! that worked. Is that how it should be done?
– psj01
Nov 9 at 22:08




Ah! that worked. Is that how it should be done?
– psj01
Nov 9 at 22:08




1




1




It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves. If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.
– Tholle
Nov 9 at 22:12




It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves. If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.
– Tholle
Nov 9 at 22:12












1 Answer
1






active

oldest

votes

















up vote
0
down vote














It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves.



import charlie1 from './charlie1.jpg';

<img src=charlie1 />


If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.



package.json




"homepage": "https://xyz.github.io/charlieReact"



App.js



<img src=`$process.env.PUBLIC_URL/img/charlie1.jpg` />





share|improve this answer






















  • I already have that in my package.json file
    – psj01
    Nov 9 at 21:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233668%2fproblem-with-relative-path-when-deploying-react-app-to-a-subfolder-in-github-pag%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote














It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves.



import charlie1 from './charlie1.jpg';

<img src=charlie1 />


If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.



package.json




"homepage": "https://xyz.github.io/charlieReact"



App.js



<img src=`$process.env.PUBLIC_URL/img/charlie1.jpg` />





share|improve this answer






















  • I already have that in my package.json file
    – psj01
    Nov 9 at 21:53














up vote
0
down vote














It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves.



import charlie1 from './charlie1.jpg';

<img src=charlie1 />


If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.



package.json




"homepage": "https://xyz.github.io/charlieReact"



App.js



<img src=`$process.env.PUBLIC_URL/img/charlie1.jpg` />





share|improve this answer






















  • I already have that in my package.json file
    – psj01
    Nov 9 at 21:53












up vote
0
down vote










up vote
0
down vote










It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves.



import charlie1 from './charlie1.jpg';

<img src=charlie1 />


If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.



package.json




"homepage": "https://xyz.github.io/charlieReact"



App.js



<img src=`$process.env.PUBLIC_URL/img/charlie1.jpg` />





share|improve this answer















It's a good idea to import the images into the JavaScript and use the imported variable whenever you can, and the URLs will just work by themselves.



import charlie1 from './charlie1.jpg';

<img src=charlie1 />


If your page has a root other than / and you have assets in the public directory, you most likely have to do some extra logic in the code.



package.json




"homepage": "https://xyz.github.io/charlieReact"



App.js



<img src=`$process.env.PUBLIC_URL/img/charlie1.jpg` />






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 22:28

























answered Nov 9 at 21:53









Tholle

33k53558




33k53558











  • I already have that in my package.json file
    – psj01
    Nov 9 at 21:53
















  • I already have that in my package.json file
    – psj01
    Nov 9 at 21:53















I already have that in my package.json file
– psj01
Nov 9 at 21:53




I already have that in my package.json file
– psj01
Nov 9 at 21:53

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233668%2fproblem-with-relative-path-when-deploying-react-app-to-a-subfolder-in-github-pag%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo