Introduction to Nuxt.js and Installation
🎯 Introduction
By the end of this article, you will understand what Nuxt.js is, the problems it solves, and why it is a common choice for server-side rendering or static generation in Vue 3 projects. You will also create and run your first Nuxt project with the official scaffolding tool and become familiar with its directory structure and common commands.
🧱 What Is Nuxt.js?
Nuxt.js is a full-stack framework built on Vue 3. It extends Vue with an integrated approach to routing, server-side rendering, static generation, auto-imports, deployment, and other application infrastructure.
Think of it as Vue with a complete set of application features:
- File-based routing: Create a
.vuefile inpages/, and Nuxt automatically creates a route for it. - Auto-imports: Many components, composables, and utilities are imported automatically, reducing repetitive
importstatements. - Flexible rendering: Choose server-side rendering (SSR), static site generation (SSG), or client-side rendering (CSR).
- Deployment support: Built-in commands produce output for Node.js servers, static hosting, serverless platforms, and more.
✨ Why Choose Nuxt.js?
| Feature | Standard Vue 3 + Vite project | Nuxt.js |
|---|---|---|
| Routing | Configure vue-router manually | Generated from the file system |
| Component/utility imports | Manual import statements | Automatic in many cases |
| SEO for initial content | Requires additional setup | Built-in SSR / SSG |
| Project conventions | Defined by each developer | Shared directory conventions simplify teamwork |
| Deployment | Set up manually | Multiple built-in deployment presets |
🚀 Install and Create Your First Nuxt Project
Nuxt provides the official nuxi scaffolding tool to create projects. This course uses Node.js v22.0.0 and Nuxt ^3.16.0 (Nuxt 3 requires Node.js >= 18.0.0).
Run the following command to create a project:
npx nuxi@latest init my-nuxt-app
nuxi is Nuxt's official CLI. It guides you through choosing a package manager, installing dependencies, and other setup steps. You can accept the defaults throughout.Enter the project directory and install dependencies:
cd my-nuxt-app
npm install
Start the development server:
npm run dev
Open http://localhost:3000/, the default address, to see Nuxt's welcome page.
📁 Project Structure
A basic Nuxt project has the following structure:
my-nuxt-app/
├─ .nuxt/ // Generated runtime directory for development
├─ app/
│ ├─ components/ // Auto-imported components
│ ├─ pages/ // File-based route pages
│ ├─ app.vue // Root application component
├─ public/ // Static assets deployed directly
├─ nuxt.config.ts // Nuxt configuration file
├─ package.json // Project dependencies and scripts
app/app.vue, rather than the src/main.js used in a traditional Vue project. Files in pages/ are automatically registered as routes.🛠 Common Commands
After installation, package.json includes several common scripts:
{
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
}
}
npm run dev: Start the development server with hot module replacement.npm run build: Create a production build (SSR mode).npm run generate: Generate a static site (SSG mode) for static hosting.npm run preview: Preview the production build locally.
❓ Questions and Answers
Q1: Is Nuxt.js only for Vue projects?
Yes. Nuxt is designed specifically for the Vue ecosystem and is primarily based on Vue 3.
Q2: How are Nuxt and Vite related?
Nuxt 3 uses Vite as its default build tool, so the development experience, including hot module replacement, is similar to a Vite project.
Q3: When is Nuxt unnecessary?
For a very small single page that needs neither routing nor SEO, Vite + Vue 3 is a lighter option.
Q4: How do npm run generate and npm run build differ?
build produces runtime output that supports server-side rendering. generate renders pages as static HTML at build time for static hosting.
🧪 Practice
Follow these steps to create your first Nuxt project:
- Create a project with
npx nuxi@latest init my-nuxt-app(Node.js v18 or later is recommended). - Enter the directory and run
npm installto install dependencies. - Run
npm run devand check thathttp://localhost:3000/loads. - Open
app/app.vueand replace the welcome page heading with your name.
<template>
<div>
<h1>Change the heading here</h1>
<NuxtPage />
</div>
</template>
🎉 You have completed your introduction to Nuxt.js! Next, we will explore directory conventions and auto-imports to see how convention over configuration simplifies development.
