Configuration and Deployment

Learn common nuxt.config.ts options, runtime configuration, rendering modes, and how to deploy Nuxt projects.

🎯 Introduction

By the end of this article, you will be able to configure Nuxt and choose a rendering mode and deployment approach for your project. You will learn common nuxt.config.ts options, how to use runtimeConfig, and the differences between SSR, SSG, and CSR.


🧱 What Is nuxt.config.ts?

nuxt.config.ts is the main configuration file for a Nuxt project. It controls modules, runtime settings, build behavior, the development server, and more.

A basic configuration looks like this:

nuxt.config.ts
export default defineNuxtConfig({
    devtools: { enabled: true },
    modules: ['@pinia/nuxt'],
    css: ['~/assets/css/main.css'],
    app: {
        head: {
            title: 'My Nuxt Blog',
            meta: [{ name: 'description', content: 'A technical blog built with Nuxt' }],
        },
    },
});
~/ is a built-in Nuxt alias for the source directory. Use it to reference files in directories such as assets/ and components/.

⚙️ Common Configuration Options

css: Global Styles

Add stylesheets to the css array to apply them across all pages:

nuxt.config.ts
export default defineNuxtConfig({
    css: ['~/assets/css/main.css'],
});

app.head: Global Head Metadata

Configure site metadata, including the title, description, favicon, and SEO information:

nuxt.config.ts
export default defineNuxtConfig({
    app: {
        head: {
            title: 'My Blog',
            meta: [{ name: 'description', content: 'Sharing frontend development knowledge' }],
            link: [{ rel: 'icon', href: '/favicon.ico' }],
        },
    },
});

modules: Extending Nuxt

Nuxt modules extend the framework. Integrations such as Pinia, Content, and Image are added as modules:

nuxt.config.ts
export default defineNuxtConfig({
    modules: ['@pinia/nuxt', '@nuxt/content'],
});

🔐 runtimeConfig: Runtime Configuration

Use Nuxt's runtimeConfig for settings you do not want to hard-code, such as API URLs and secrets.

nuxt.config.ts
export default defineNuxtConfig({
    runtimeConfig: {
        // Private to the server; not exposed to the client
        apiSecret: 'xxxxxx',
        // Accessible to the client; must be inside public
        public: {
            apiBase: 'https://api.example.com',
        },
    },
});

Use it in a component:

app/pages/index.vue
<script setup>
const config = useRuntimeConfig();

console.log(config.public.apiBase); // Accessible on both the client and server
console.log(config.apiSecret);      // Accessible only on the server
</script>
Do not put sensitive information in public, because it is exposed to the client.

🌐 Three Rendering Modes

Nuxt supports three rendering modes. Choose one based on your application's requirements:

ModeDescriptionSuitable use cases
SSRServer-side rendering generates HTML for each requestFrequently changing content, SEO, and authenticated users
SSGGenerates static HTML at build timeInfrequently changing content, blogs, and documentation
CSRRenders entirely on the clientAdmin dashboards and internal tools with little need for SEO
Nuxt uses SSR by default unless you configure a different rendering mode.

🚀 Deployment Options

SSR Deployment

For a Node.js SSR deployment, build the project with:

npm run build

After the build completes, run:

node .output/server/index.mjs
npm run build creates .output/ with both server and client assets. Deploy this directory to a server that supports Node.js.

SSG Static Deployment

For a fully static site, run:

npm run generate

This generates static HTML and assets that you can upload to a CDN or a static hosting platform such as GitHub Pages or Vercel. The static output directory is .output/public/.

When using generate, configure nitro.prerender.routes for dynamic routes that might otherwise be missed.

❓ Questions and Answers

Q1: How do runtimeConfig and app.config differ?

Use runtimeConfig for environment-dependent settings, such as API URLs and secrets. Use app.config for public application settings, such as theme names and version numbers.

Q2: How do I handle dynamic routes with SSG?

List the dynamic paths to prerender in nitro.prerender.routes in nuxt.config.ts.

Q3: Can I deploy Nuxt to static hosting?

Yes. Run npm run generate and upload the generated .output/public/ directory to your static hosting platform.

Q4: Is SSR or SSG better for SEO?

Both produce HTML and work well for SEO. SSR suits frequently changing content, while SSG suits stable content.


🧪 Practice

Complete the following configuration tasks:

  1. Set the global site title to “My Nuxt Site” in nuxt.config.ts.
  2. Add runtimeConfig.public.apiBase with the value https://jsonplaceholder.typicode.com.
  3. Replace the hard-coded API URL in app/pages/index.vue with useRuntimeConfig().public.apiBase.
  4. Run both npm run build and npm run generate and compare the server build in .output/ with the static output in .output/public/.
nuxt.config.ts
export default defineNuxtConfig({
    // Configure the global title here
    runtimeConfig: {
        public: {
            // Configure apiBase here
        },
    },
});

🎉 You have completed the Nuxt.js introductory course! You now understand directory conventions, routing, data fetching, state management, and deployment configuration, and are ready to build your own Nuxt projects.