Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the publicPath in Create React App

I want to change the root or public path of my app from / to /something. How? This makes no sense based on the title.

I'm using React Router so this docs cannot be used as in to set the "homepage" in package.json.

I've added webpack.config.js to the root of the folder:

module.exports = {
    publicPath: '/v2/'
}

Restarted and my asset, manigest etc files are still pointing at the root. This example worked with Vuejs in vue.config.js. How to achieve this without ejecting? I got publicPath from here.

like image 689
Sylar Avatar asked Sep 18 '25 04:09

Sylar


1 Answers

in your root project add a file called .env, inside the file enter the path you want:

PUBLIC_URL=/v2/

this should fix your problem.

if you want to modify webpack.config.js in a react project created with npx create-react-app I suggest you to install craco https://www.npmjs.com/package/@craco/craco .

after the installation you need to replace some lines of your package.json

from

...
  "scripts": {
    "start": "react-script start",
    "build": "react-script build",
    "test": "react-script test",
    "eject": "react-script eject"
  },
...

to

...
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "craco eject"
  },
...

after that, in your root project add a file called craco.config.js

inside the file add the following code

module.exports = {
    configure: {
      output: {
        publicPath: '/v2/'
      }
    }
  }
}
like image 101
Francesco Orsi Avatar answered Sep 20 '25 19:09

Francesco Orsi