27

I am working on a react project and am running into some issues with http-proxy-middleware. I followed the readme and my setupProxy.js file looks like this

const {createProxyMiddleware} = require('http-proxy-middleware');


module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'https://localhost:3000',
      changeOrigin: true
    })
  );
};

Anyone have any ideas as to why this might be occurring? From what I've seen, this is the correct way to set this up. Any response or suggestions will be greatly appreciated.

otmaka
  • 316
  • 1
  • 3
  • 5
  • Do you see `http-proxy-middleware` in your package.json? – Sohaib Nov 17 '20 at 02:24
  • Putting this out there just in case: after installing http-proxy-middleware, check your package.json file like Sohaib said then be sure to restart both of the servers!! – otmaka Jan 04 '21 at 19:23

2 Answers2

49

Removing the brackets works for me

const createProxyMiddleware = require('http-proxy-middleware');

Found that fix here https://www.reddit.com/r/reactjs/comments/jzoo3y/createproxymiddleware_is_not_a_function_how_can_i/

Kyle Ledbetter
  • 1,201
  • 10
  • 7
8

If all React js component files are in the component folder(or in another folder) then shift your Setupproxy.js file in that folder now you can use

 const { createProxyMiddleware } = require('http-proxy-middleware');

For global, put your Setupproxy.js file in src folder then you have to remove {}

const createProxyMiddleware = require('http-proxy-middleware');
Syed Ali Shahzil
  • 1,079
  • 1
  • 11
  • 17
  • 3
    Why would it depend on whether there are other files in the same folder? Can you explain? – Phil Aug 26 '21 at 07:25