reactjsproxyvite

react.js - Proxy Setup Not Working With Vite


I'm new to React.js and have been learning from various tutorials. I was trying to setup a proxy for a local-server based API call however I get this error after running the 'npm run dev' command.

Cannot create property 'prependPath' on boolean 'true'

I'm using Vite and have my proxy settings/code as

proxy: {
  '/api': 'http://localhost:8000',
  changeOrigin: true,
  rewrite: (path) => path.replace(/^\/api/, '')
}

I'm not sure what the error means or how to fix it seeing that I'm still learning, can anyone help me with this?


Solution

  • According to the doc, changeOrigin and rewrite should be keys under the value of '/api':

    proxy: {
      '/api': {
        target: 'http://localhost:8000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }