Configure nuxt.js application to work in a subdirectory on a webserver

13,388

Solution 1

You need router/base

router: {
  base: '/dist/'
}

Solution 2

To complete @Aldarund answer, I use :

export default {
[…] // some code
  router: {
    base:
      process.env.NODE_ENV === "development" ? process.env.BASE_URL : "/<subfolder>/"
  } // where <subfolder> is the subfolder!
};
Share:
13,388
Marc
Author by

Marc

Updated on June 05, 2022

Comments

  • Marc
    Marc almost 2 years

    I want to deploy a static nuxt.js application (built with nuxt generate) to a subdirectory of a webserver. nuxt places the generated files in the dist directory by default:

    default output

    If I start a webserver on the parent directory of the dist folder and open the page with:

    http://localhost:34360/dist/

    the site fails to load the script files from the domain root directory:

    404 Default

    I've tried setting the publicPath property in the nuxt config:

    build: {
      publicPath: '/dist/'
    }
    

    The appplication compiles to:

    output with publicPath

    Now, nuxt moves the script files one level lower (/dist/dist) and searches on root level again (/dist), thus still not finding the files

    error with publicpath

    How can I configure the site, such that scripts and assets are loaded and it is self contained, no matter on which directory on my server I put it?

    The issue has been covered on GitHub but the suggested hints (using publicPath) didn't work, as shown above.

    Sidenote: I do not want to specify the publicPath absolut (i.e. http://localhost:8080/dist), which would work but creates new problems.

  • Marc
    Marc almost 6 years
    This is what I was searching for! Thank you! And is there a way to configure this generically, i.e. in a the app could be moved to /xyz/ without requiring changing the config and rebuilding?
  • Aldarund
    Aldarund almost 6 years
    without rebuilding i dont think so. maybe only via some 3rd party module. As for dynamically you could do it in nuxt.config build.extend
  • Marc
    Marc almost 6 years
    Thank you, I will fo with your solution for now!
  • h3nr1ke
    h3nr1ke over 3 years
    for future needs, here is the documentation about router base configuration nuxtjs.org/docs/2.x/configuration-glossary/configuration-rou‌​ter
  • Earth_Believer
    Earth_Believer about 3 years
    This is what I was looking for. Ty.