Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue devServer.proxy in vue.config.js not working

Tags:

vue.js

vite

I'm using the following configuration in my vue.config.js located in the root of my repository and it's not working.

  module.exports = {
    devServer: {
        proxy: "http://localhost:3030"
      }
  }

and this is how I'm trying to call it

  return await fetch("/posts", options).then((response) =>
    response.json()
  ).catch(e => console.log("Error fetching posts", e));

however when I change the calling code to the code shown below everything works

  return await fetch("http://localhost:3030/posts", options).then((response) =>
    response.json()
  ).catch(e => console.log("Error fetching posts", e));

Edit:

I should have mentioned that I'm using Vite for builds as that was causing some other problems for me with environment variables so it's possible they are causing problems with proxies too.

I looked into this more and it turns out that Vite does have proxy features and so I tried updating my code to use their proxy with still no luck.

// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    "/posts": "http://localhost:3030/posts"
  }
})
like image 936
Ryan Vice Avatar asked Oct 28 '25 14:10

Ryan Vice


1 Answers

vue.config.js is intended for Vue CLI scaffolded projects (and your config would've worked there), not for Vite projects. Vite's configuration is stored in vite.config.js.

Your Vite config value for server.proxy contains an unnecessary /posts suffix:

"/posts": "http://localhost:3030/posts"
                                ^^^^^^

The value should just be the base URL to which the original path is appended:

"/posts": "http://localhost:3030"

Example:

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    proxy: {
      '/posts': 'http://localhost:3030'
    }
  }
})

GitHub demo

like image 96
tony19 Avatar answered Oct 31 '25 04:10

tony19



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!