Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React App - createProxyMiddleware is not a function

Tags:

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.

like image 500
otmaka Avatar asked Nov 17 '20 00:11

otmaka


2 Answers

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/

like image 62
Kyle Ledbetter Avatar answered Sep 18 '22 13:09

Kyle Ledbetter


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'); 
like image 32
Syed Ali Shahzil Avatar answered Sep 19 '22 13:09

Syed Ali Shahzil