Directory Conventions and Auto-imports
🎯 Introduction
By the end of this article, you will understand the role of each directory in a Nuxt project and why many components and functions work without explicit imports. You will also learn when to rely on auto-imports and when explicit imports help keep code clear and reliable.
🧱 Nuxt Directory Conventions
Nuxt follows convention over configuration: place files in the appropriate directories, and the framework recognizes their roles automatically. Unlike a plain Vite project, you do not need to register routes, components, or plugins manually.
A typical Nuxt project has the following structure:
my-nuxt-app/
├─ app/
│ ├─ app.vue // Application entry point
│ ├─ pages/ // Page routes
│ ├─ components/ // Auto-imported components
│ ├─ composables/ // Auto-imported composables
│ ├─ utils/ // Auto-imported utilities
│ ├─ layouts/ // Layouts
│ ├─ middleware/ // Route middleware
│ ├─ plugins/ // Plugins
├─ public/ // Static assets
├─ server/ // Server API
├─ nuxt.config.ts // Configuration file
├─ package.json
Let's look at the most commonly used directories.
📁 app.vue: The Application Entry Point
The root component in this Nuxt 3 project is app/app.vue, equivalent to src/App.vue in a standard Vue project.
<template>
<div>
<NuxtPage />
</div>
</template>
<NuxtPage /> is a built-in Nuxt component that renders the page matching the current route. Without it, pages in pages/ will not appear.
app.vue and no pages/ behaves like a single-page Vue application built with Vite.📁 pages/: File-based Routing
Create a .vue file in pages/, and Nuxt automatically registers a route for it.
pages/
├─ index.vue // Maps to /
├─ about.vue // Maps to /about
├─ user/
│ ├─ index.vue // Maps to /user
│ ├─ [id].vue // Maps to /user/:id
vue-router configuration from pages/.📁 components/: Component Auto-imports
In a standard Vue project, each page must import its components explicitly:
<script setup>
import MyButton from '../components/MyButton.vue';
</script>
In Nuxt, components placed in app/components/ can be used directly in templates, without an import statement.
<template>
<div>
<MyButton>Click me</MyButton>
</div>
</template>
app/components/. Components placed elsewhere still need explicit imports.📁 composables/: Composable Auto-imports
In Vue 3, reusable logic is commonly extracted into composables. In Nuxt, composables placed in app/composables/ are available directly in all components.
export function useCounter() {
const count = ref(0);
const add = () => count.value++;
return {
count,
add,
};
}
Use it directly in a page:
<template>
<p>Count:{{ count }}</p>
<button @click="add">Increment</button>
</template>
<script setup>
const { count, add } = useCounter();
</script>
use, such as useUser or useFetchData. This follows community conventions and matches Nuxt's built-in composables, such as useFetch and useRoute.📁 utils/: Utility Auto-imports
Pure utility functions that do not depend on Vue's reactivity system can go in app/utils/ and are also auto-imported.
export function formatDate(date) {
return new Date(date).toLocaleDateString();
}
Use it directly in a component or composable:
<template>
<p>Today:{{ formatDate(new Date()) }}</p>
</template>
📁 public/ vs. assets/
Nuxt has two common locations for static assets, which are easy to confuse:
| Directory | Purpose | Access |
|---|---|---|
public/ | Files that do not require bundling, such as favicon.ico, images, and fonts | Access directly at /filename |
assets/ | Files processed by the build tool, such as Sass and images requiring optimization | Import through ~/assets/xxx |
For example, place logo.png at public/logo.png and reference it directly in a page:
<template>
<img src="/logo.png" alt="logo" />
</template>
public/, because they bypass Nuxt's build optimizations.🧰 How Auto-imports Work
Auto-imports work by scanning directories such as components/, composables/, and utils/ at startup and mapping names to file paths. Although you do not write imports yourself, the build tool adds them for you.
- When function names may conflict.
- In unit tests or library code, where auto-imports may not be available.
- When team conventions prioritize explicit dependencies for readability.
❓ Questions and Answers
Q1: Are components in subdirectories of app/components/ also auto-imported?
Yes. Nuxt recursively scans .vue files in components/ and derives component names from their filenames.
Q2: How do composables/ and utils/ differ?
Use composables/ for logic involving Vue reactivity, such as ref and computed. Use utils/ for pure utility functions that do not depend on Vue.
Q3: Do auto-imports work in production?
Yes. Auto-imports are part of Nuxt's build process and work in both development and production.
Q4: Can I disable auto-imports?
Set components: false or imports: { autoImport: false } in nuxt.config.ts, although disabling them is generally unnecessary.
🧪 Practice
Complete the following tasks in your existing Nuxt project:
- Create
AppAlert.vueinapp/components/to display a short message. - Create
useCounter.jsinapp/composables/, returningcountandadd. - Create
formatPrice.jsinapp/utils/to format numbers as¥xx.xx. - Use all three auto-imported items in
app/pages/index.vuewithout writing anyimportstatements.
<template>
<div>
<AppAlert />
<p>Price:{{ formatPrice(19.9) }}</p>
<p>Count:{{ count }}</p>
<button @click="add">Increment</button>
</div>
</template>
<script setup>
// Use the auto-imported useCounter here; no import is needed
</script>
🎉 You now understand Nuxt's directory conventions and auto-imports! Next, we will explore file-based routing, navigation, and dynamic parameters.
