My projects runs correctly while i am using npm run dev, but when i finished it, and use the command npm run build all the relatives routes were incorrect at dist folder (files routes like .js and .css, images, etc), and my app doesnt’t start by the "/".
pd: I’m using react-router dom.
I change all relatives routes but i’cant get it start for the root, the app start for the route: dist/index.html
I try to change the .js file in the dist folder but has a lot of code and I don’t know what i must change
2
Answers
Sorry for posting this as an answer, I can’t post it as a comment because I do not have enough reputation. With only the provided information I can advise you to check your
base
/basename
in your vite config and in the browser props.If you are using BrowserRouter from
react-router-dom
, make sure that you have configured the basename correctly, both in yourvite.config.js
and where you render the BrowserRouter in your code. The 2 values should match.Example:
App
vite.config.js
-Check your package.json file to make sure that the "homepage" field is set to the correct URL where your app will be hosted. For example, if you plan to host your app at https://example.com/my-app/, then your "homepage" field should be set to "https://example.com/my-app/".
-Make sure that your BrowserRouter component in index.js has the correct basename prop set to the same URL as your "homepage" field in package.json. For example:
-Check that your relative paths in your code are correct and relative to the root of your app. For example, if you have an image file located at public/images/logo.png, you can reference it in your code like this:
-If you are still having issues with relative paths, you can try using absolute paths instead. One way to do this is by creating a src/config.js file with the following content:
-Then, you can import this file in your components and use the absolute paths like this:
I hope these steps help you resolve your issue with relative paths in your React project!