Directory Conventions and Auto-imports

Learn Nuxt's directory conventions and auto-imports, understand each directory's role, and reduce unnecessary 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.

app/app.vue
<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.

A simple project with only 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
We will cover file-based routing in the next article. For now, remember that Nuxt generates the 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.

app/pages/index.vue
<template>
    <div>
        <MyButton>Click me</MyButton>
    </div>
</template>
Component auto-imports recognize components in 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.

app/composables/useCounter.js
export function useCounter() {
    const count = ref(0);
    const add = () => count.value++;

    return {
        count,
        add,
    };
}

Use it directly in a page:

app/pages/index.vue
<template>
    <p>Count:{{ count }}</p>
    <button @click="add">Increment</button>
</template>

<script setup>
const { count, add } = useCounter();
</script>
Start composable names with 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.

app/utils/formatDate.js
export function formatDate(date) {
    return new Date(date).toLocaleDateString();
}

Use it directly in a component or composable:

app/pages/index.vue
<template>
    <p>Today:{{ formatDate(new Date()) }}</p>
</template>

📁 public/ vs. assets/

Nuxt has two common locations for static assets, which are easy to confuse:

DirectoryPurposeAccess
public/Files that do not require bundling, such as favicon.ico, images, and fontsAccess directly at /filename
assets/Files processed by the build tool, such as Sass and images requiring optimizationImport 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>
Avoid placing assets that require versioning or build processing in 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.

Auto-imports are convenient, but explicit imports are preferable in some situations:
  • 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:

  1. Create AppAlert.vue in app/components/ to display a short message.
  2. Create useCounter.js in app/composables/, returning count and add.
  3. Create formatPrice.js in app/utils/ to format numbers as ¥xx.xx.
  4. Use all three auto-imported items in app/pages/index.vue without writing any import statements.
app/pages/index.vue
<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.