41

I'm experiencing problems deploying a Vue JS app built using the Webpack CLi to work.

If uploaded in a root directory everything renders fine, but inside a subfolder, all the links break.

I've added a base href:

    <base href="/dist/">

And the scripts load, but all the asset paths created by Webpack are broken, the images and fonts don't load as they are pointing to the root directory.

Can anyone please help?

4 Answers 4

46

For Vue CLI 3 it is pretty simple.

Edit your vue.config.js (if there is none, create it in project root directory) and add following lines:

module.exports = {
  baseUrl: "./"
};

Or whatever sub-directory you want.

You may also decide according to NODE_ENV. See the docs.

module.exports = {
  baseUrl: process.env.NODE_ENV === 'production'
    ? '/production-sub-path/'
    : '/'
}

UPDATE

As mentioned in comments below, for Vue CLI 3.3+ use publicPath property instead of baseUrl.

Sign up to request clarification or add additional context in comments.

3 Comments

baseUrl is now depracated, in favor of publicPath. Works fine!
I just ran into this problem today. I needed this answer. Thanks!
@jedik Thanks for the update. I confirm that this works in vue3
43

Assuming you are using the webpack template from vue-cli, you need to edit the assetsPublicPath property in config/index.js - notice there is one for build and for dev

Check out Handling Static Assets section in the docs for more info.

Update:

Newer link for CLI v3+ users: https://cli.vuejs.org/guide/html-and-static-assets.html

Notice the property is just called publicPath

2 Comments

I end up configuring the assetsPublicPath to . (dot) and no <base href=''> to make it executable from any path
how to do it with vue-cli 3?
2

If you installed your app with

npm install -g @vue/cli vue ui then you should create file vue.config.js in the top project folder (where package.json is) and paste like above.

Comments

2

For Vue 3 projects scaffolded with Vite, edit the vite.config.js file, adding/changing the base property as following:

export default defineConfig({
  /* ... */

  base: './',

  /* ... */
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.