Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing proxy in package.json is not working

I created a project using create-react-app and this one is running on http://localhost:3000/

when i want make a request to http://localhost:3090/ from my application i am setting proxy in my package.json file which is not working

componentDidMount() {
     fetch('/api/si')
      .then(response => {
        console.log(response);
        return response.json();
      })
}

package.json

"proxy":"http://localhost:3090/api"

here my expected call is localhost:3090/api/si but it's pointing to 3000 which is my client server. I tried multiple combinations nothing works


like image 791
Raveendra Reddy Avatar asked Sep 13 '25 20:09

Raveendra Reddy


2 Answers

Try Removing /api from

"proxy":"http://localhost:3090/api"

or else try

componentDidMount() {
 fetch('/si')
  .then(response => {
    console.log(response);
    return response.json();
  })

}

Change one of them

In case of Multiple Proxies you can do something like this:

"proxy": {
  "/auth/*": {
    "target": "http://localhost:5000"
  },
  "/api/*": {
    "target": "http://localhost:3090"
  }
}
like image 106
Saqib Hussain Avatar answered Sep 16 '25 08:09

Saqib Hussain


The fix in my case was to put "proxy" below "scripts" in react-app package.json file. Initially I was putting it at the end of file below "development" tag. I don't know how it worked but it did. This is what I did:

  1. moved "proxy" tag below "scripts" tag in react package.json file
  2. deleted react package-lock.json file
  3. deleted react node modules
  4. executed 'npm install' to re-install node modules
  5. Run react app then It worked.

My package.json file which was working is:

  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.14.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "axios": "^0.21.1",
    "react": "^17.0.2",
    "react-bootstrap": "^1.6.1",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000/",
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
like image 34
Pushpendra Kumar Avatar answered Sep 16 '25 10:09

Pushpendra Kumar