Create-react-app: Upgrading from react-script 3.3.1 to react-scripts 3.4.0 required changing setupProxy

Created on 19 Feb 2020  路  4Comments  路  Source: facebook/create-react-app

Not 100% sure why -- but after upgrading to react-scripts 3.4.0 it became necessary for me to change the way that I'm requiring the http proxy ...

The instructions from here -- https://create-react-app.dev/docs/proxying-api-requests-in-development/ -- fail with 'proxy is not a function'

I had to change this code (which was working before ...):

const proxy = require("http-proxy-middleware");

const setupBackend = require("./setupBackend");
const target = setupBackend();

module.exports = function(app) {
  console.log("Setting up proxy url's on development server");

  app.use(
    ["/token_api", "/__meta_request"],
    proxy({
      target,
      changeOrigin: true,
      preserveHeaderKeyCase: false
    })
  );
};

To this:

const proxy = require("http-proxy-middleware").createProxyMiddleware;

const setupBackend = require("./setupBackend");
const target = setupBackend();

module.exports = function(app) {
  console.log("Setting up proxy url's on development server");

  app.use(
    ["/token_api", "/__meta_request"],
    proxy({
      target,
      changeOrigin: true,
      preserveHeaderKeyCase: false
    })
  );
};
bug report needs triage

Most helpful comment

https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually is now wrong due to the breaking changes in http-proxy-middleware.

I opened https://github.com/facebook/create-react-app/pull/8515 to fix.

All 4 comments

Your two examples are identical. Did you accidentally paste the same code twice?

@partynikko oops I did ... edited.

The original code I had to change used the default export off of the http-proxy-middleware ...

const proxy = require("http-proxy-middleware");

const setupBackend = require("./setupBackend");
const target = setupBackend();

module.exports = function(app) {
  console.log("Setting up proxy url's on development server");

  app.use(
    ["/token_api", "/__meta_request"],
    proxy({
      target,
      changeOrigin: true,
      preserveHeaderKeyCase: false
    })
  );
};

This has nothing to do with CRA but with http-proxy-middleware package. They just released a new major version introducing breaking changes like the one you are referring to.

Could it be that you probably upgraded that package and confused it with the update of react-scripts?

https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually is now wrong due to the breaking changes in http-proxy-middleware.

I opened https://github.com/facebook/create-react-app/pull/8515 to fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexeyRyashencev picture AlexeyRyashencev  路  3Comments

oltsa picture oltsa  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

rdamian3 picture rdamian3  路  3Comments

ap13p picture ap13p  路  3Comments