Learning objectives
After reading this article you will be able to:
- Choose between an SPA, Nuxt with SSR and a site built with nuxt generate
- Build and start the Nuxt server with the right port and host
- Set environment variables without leaking secrets to the browser
On this page
- Which case is yours: SPA, Nuxt with SSR or static Nuxt?
- How do you host a Vue app built with Vite?
- How do you host a Nuxt app with SSR?
- How do you build a static site with nuxt generate?
- How do you set environment variables in Vue and Nuxt?
- What are the most common errors?
- How to do this on Square Cloud
- FAQs
"Hosting a Vue app" can mean three quite different things. An SPA built with Vite becomes a folder of files that any static server can deliver. A Nuxt project with server-side rendering needs a Node.js process that stays up all the time. And the same Nuxt project can become a static site, with a ready HTML file for each route.
Before you pick a host, find out which of the three your project is. Everything else (the build command, the folder that ships, the port, the variables) follows from that.
Which case is yours: SPA, Nuxt with SSR or static Nuxt?#
| Case | Build command | What ships to production | Needs Node.js in production? |
|---|---|---|---|
| Vue with Vite (SPA) | npm run build |
Contents of dist/ |
No |
| Nuxt with SSR | nuxt build |
The .output/ folder |
Yes |
| Prerendered Nuxt | nuxt generate |
Contents of .output/public/ |
No |
Client-only Nuxt (ssr: false) |
nuxt generate |
Contents of .output/public/ |
No |
The SPA is the simplest path, but the HTML arrives empty and the content only shows up once the JavaScript runs. SSR delivers a finished page on every request, which helps SEO and content that changes all the time. A prerendered site delivers finished HTML with no server, as long as the pages can be generated ahead of time. The full comparison is in SSR vs SSG vs static.
In every case, use a recent Node.js version on your machine and in CI. Each tool's minimum version is in the Vue and Nuxt documentation; the active LTS usually covers both.
How do you host a Vue app built with Vite?#
Create the project with the official scaffolding tool, which asks for a name and whether you want Vue Router, TypeScript and other extras:
npm create vue@latest
cd my-app
npm install
npm run build
npm run previewnpm run build writes the minified result to dist/. npm run preview serves that folder at http://localhost:4173 so you can check the build, but it is not a production server. What you publish is the contents of dist/, with index.html at the root. If the site lives under a subpath (example.com/app/), set base: '/app/' in vite.config.js.
With Vue Router in history mode, routes only exist in JavaScript:
import { createRouter, createWebHistory } from "vue-router";
import Home from "./views/Home.vue";
export const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{ path: "/", component: Home },
{ path: "/about", component: () => import("./views/About.vue") },
{
path: "/:pathMatch(.*)*",
component: () => import("./views/NotFound.vue"),
},
],
});Clicking a link works because the browser asks the server for nothing. Reloading /about sends a real request, and a static server with no file at that path answers 404. The fix the Vue Router documentation gives is a fallback on the server: it returns index.html for every path that is not a file, and the router picks the screen. The last route above shows a "not found" screen inside the app, since the server now answers 200 for any path. The setup for nginx, Apache and others is in 404 on refresh in an SPA.
How do you host a Nuxt app with SSR?#
Create the project and run the production build:
npm create nuxt@latest my-app
cd my-app
npm run buildnuxt build creates the .output/ folder, with the server at .output/server/index.mjs and the public files in .output/public/. The documentation of Nitro, the server engine Nuxt uses, says this folder is standalone and can be deployed to any host. To start it:
NODE_ENV=production node .output/server/index.mjsThe server reads the port from NITRO_PORT or PORT (default 3000) and the address from NITRO_HOST or HOST, in that order of priority. The Nuxt documentation gives 0.0.0.0 as the default address, which accepts connections from outside the container. If your host sets HOST to something else, pin the address in the command and let the port come from the PORT your host sets:
NODE_ENV=production NITRO_HOST=0.0.0.0 node .output/server/index.mjsNODE_ENV=production keeps Vue Router from flooding the logs with warnings, according to the Nuxt documentation. If the process listens only on 127.0.0.1 or on a port other than the one your host forwards, the app looks down; the diagnosis is in EADDRINUSE, PORT and 0.0.0.0.
If you plan to build on the server itself, check package.json: the Nuxt starter project already lists nuxt under dependencies, not devDependencies. That matters on hosts that install only production dependencies. Building in CI, as in automatic deploys with GitHub Actions, and shipping the finished .output folder avoids the issue and saves memory on the server.
How do you build a static site with nuxt generate?#
If your pages can be generated ahead of time, you can skip the server:
npm run generatenuxt generate prerenders your routes into HTML and writes everything to .output/public/, which you publish on any static server. The generator starts at the root and at the pages without parameters in pages/, then follows the <a href> links it finds. A dynamic route no page links to (a /blog/[slug] missing from every index, for example) is not generated and has to be added to prerender.routes in nuxt.config.
For a pure SPA with no rendered HTML, turn SSR off:
// nuxt.config.ts
export default defineNuxtConfig({
ssr: false,
});With ssr: false, nuxt generate creates an entry index.html plus the JavaScript bundles, like a classic Vue app. In both modes it also writes a 200.html (the SPA fallback, for routes the client-side router resolves) and a 404.html (for routes that should stay 404). Point your static server at the right file; every host has its own fallback or rewrite setting.
How do you set environment variables in Vue and Nuxt?#
In Vue with Vite, only variables prefixed with VITE_ reach your code, and they are replaced at build time: the value is baked into the JavaScript, any visitor can read it, and changing it needs a new build. Never put secret keys in VITE_*. The difference between the two moments is covered in build-time vs runtime variables.
In Nuxt with SSR, declare your keys in runtimeConfig:
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
apiSecret: "",
public: {
apiBase: "",
},
},
});In production, variables with the NUXT_ prefix override those values at runtime, with no new build:
NUXT_API_SECRET=replace-with-a-secret NUXT_PUBLIC_API_BASE=https://api.example.com node .output/server/index.mjsIn your code, useRuntimeConfig() returns the values. Keys inside public reach the browser; the others stay on the server. Two details trip people up: after the build, Nuxt no longer reads the .env file, and a variable without the NUXT_ prefix does not override runtimeConfig. A site built with nuxt generate has no server in production, so whatever was read during generation stays in the files.
What are the most common errors?#
- Blank page and 404s on
/assetsor/_nuxtfiles: the base path doesn't match the site's address. Fix Vite'sbaseor Nuxt'sapp.baseURL. - 404 when reloading a route: the fallback to
index.html(Vue) or to200.html(Nuxt withssr: false) is missing. - Nuxt app doesn't answer at the public address: the process listens on another host or port. Check
NITRO_HOST,HOST,NITRO_PORTandPORTin the environment. Cannot find moduleon start:.output/was never built or never uploaded. Run the build before the start; see npm install errors on deploy if the failure comes from the install.- Process killed during the build: usually lack of memory. Build in CI and ship the result.
- Variable with an old value: it was a
VITE_*one, baked in at build time, or it lacked theNUXT_prefix.
After deploying, open a deep route straight from the address bar and reload it. If it loads, the fallback or the server is set up right.
How to do this on Square Cloud#
All three paths fit on Square Cloud. The Vue SPA and the nuxt generate site go on the static sites runtime: a web server (nginx) stays up delivering the files, and an update shows up right away, with no rebuild or restart. For the SPA fallback, rename the build's index.html to vite.html: the runtime answers every path that is not a file with it. Without vite.html, unknown paths get the 404.html.
Nuxt with SSR goes on the Node.js runtime. There is no build step: run nuxt build in an external CI, such as GitHub Actions, and upload the finished .output folder, or put the build in the start command (START). On start, the platform runs npm install when there is no node_modules, with NODE_ENV=production, so devDependencies are not installed. With the build in START, nuxt and everything the build uses must be in dependencies (the starter project already lists nuxt there), and the build runs again on every start. The server must listen on 0.0.0.0 on the port in the PORT variable, which the platform sets.
Two example start commands, to adjust to your project: NODE_ENV=production NITRO_HOST=0.0.0.0 node .output/server/index.mjs, with .output coming from CI, or npm run build && NODE_ENV=production NITRO_HOST=0.0.0.0 node .output/server/index.mjs, with the build in START.
You can deploy by ZIP upload in the dashboard, with the CLI, from GitHub or with the VS Code extension. The squarecloud.app file is optional: the dashboard generates it at deploy time, or you write it at the root of the project. Environment variables are set per application and apply on the next restart, with no rebuild, which covers the NUXT_* ones. VITE_* values are only read when the build runs, so they belong in the environment where it happens.
A web application uses at least 512 MB of the plan's RAM pool and gets a *.squareweb.app subdomain with HTTPS. Custom domains are available from Standard, and Hobby starts at R$12.49 per month. The details are on the sites page.
Website hosting
Static and SSR sites with automatic TLS and a global CDN
FAQs
Do I need Node.js on the server to host a Vue app?
No, if the app is an SPA built with Vite. The build produces ready HTML, CSS and JavaScript that any static file server can deliver. You only need Node.js in production for Nuxt with server-side rendering (SSR).
What is the difference between nuxt build and nuxt generate?
nuxt build produces a Node.js server in the .output folder that renders pages on every request. nuxt generate prerenders your routes into HTML files in .output/public, which a static server delivers without running any code.
Which port does Nuxt use in production?
3000 by default. The server reads the port from NITRO_PORT or PORT and the address from NITRO_HOST or HOST, which the Nuxt documentation says defaults to 0.0.0.0.
Why doesn't Nuxt read my .env file in production?
Because once the server is built, .env files are no longer read. Set the variables in the process environment through your host and read them via runtimeConfig with the NUXT_ prefix (or NUXT_PUBLIC_ for public values).
More in Websites
See all 25 articles- What is a static site (and how to host one)A static site is a set of ready-made HTML, CSS and JavaScript files served the same to every visitor. Learn how it works, when to use one and how to host it.
- How much does it cost to host a website?Hosting a website costs anywhere from nothing to a few dozen dollars a month. See what goes into the bill: hosting, domain, traffic and extras, with real limits.
- How much does static site hosting cost?Hosting a static site costs between zero and a few dozen dollars a month in most cases. See how traffic sets the price, with step-by-step math.
- How much does it cost to host a Next.js app?Hosting a Next.js app costs from zero with a static export to tens of dollars a month with a server. See the billing models, traffic math and hidden costs.