Setting up a Vue project

Beginner · 9 min

Scaffolding a Vue 3 app with create-vue and Vite, what each generated file does, how the app boots from index.html, and the editor and devtools setup.

Before thisWhy Vue and the progressive framework

freeCodeCamp.org · 98 min · from 16:30

Setting up a Vue project

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by freeCodeCamp.orgReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `npm create vue@latest` runs create-vue, the official scaffolder, which asks which optional pieces you want (TypeScript, Router, Pinia, Vitest, end-to-end tests, linting) and generates a Vite-powered project around your answers.
  • Vite serves `index.html` as the real entry point; its `<script type="module" src="/src/main.js">` tag loads `main.js`, which calls `createApp(App).mount('#app')` to render the root component into the empty `<div id="app">`.
  • `npm run dev` starts a dev server that compiles `.vue` files on demand and hot-swaps edited components without a full reload; `npm run build` produces optimized static files in `dist/`, and `npm run preview` serves that build locally.
  • The generated `vite.config.js` registers `@vitejs/plugin-vue` (which compiles single-file components) and an `@` alias for `src/`, so `@/components/Foo.vue` works from any depth.
  • Vue CLI is in maintenance mode; new Vue projects use create-vue and Vite, or `npm create nuxt@latest` when you want server rendering and file-based routing from the start.
  • Install the Vue (Official) editor extension for template type-checking and autocompletion, and use Vue DevTools to inspect components, state, and events in the running app.

Why a project needs a build step at all

The previous lesson showed Vue running from a single CDN <script> tag. That works for a widget, but it falls apart once an app has dozens of components. You want each component in its own file, you want to import libraries from npm, you want TypeScript and Sass, and you want the browser to receive a small, minified bundle rather than every source file. Browsers do none of that on their own.

A build tool bridges the gap. During development it turns your source files into something the browser can run, as fast as possible; for production it bundles, tree-shakes, and minifies. In the Vue world that tool is Vite, created by Evan You alongside Vue and now used well beyond it. The general picture of bundlers and dev servers is covered in module bundlers and build tools; this lesson is about the Vue-specific setup on top.

Scaffolding with create-vue

You need a recent Node.js release first. The current create-vue declares Node ^22.18.0 || >=24.12.0 in its engines field, so an up-to-date LTS is the safe choice. If npm itself is unfamiliar, package managers and npm covers it. Then:

npm create vue@latest

Keep the @latest. Without it npm may run a cached older version of the scaffolder. The command starts create-vue, which asks a short series of questions:

◇  Project name (target directory): my-shop
◇  Select features to include in your project:
│  ◼ TypeScript
│  ◼ Router (SPA development)
│  ◼ Pinia (state management)
│  ◻ Vitest (unit testing)
│  ◻ End-to-End Testing
│  ◼ Linter (error prevention)
│  ◼ Prettier (code formatting)
◇  Skip all example code and start with a blank Vue project? No

Each answer adds files and dependencies; nothing is installed you didn’t ask for. That is the progressive idea from the last lesson, applied at scaffold time. Recent versions also offer an “experimental features” step, for example trying the Vue 3.6 release candidate. For learning, plain JavaScript with Router and Pinia is plenty; TypeScript is covered later in TypeScript with Vue. Then:

cd my-shop
npm install
npm run dev

The dev server prints a local URL, usually http://localhost:5173, and the starter page appears.

Vue CLI, the older webpack-based tool you will see in tutorials from before 2022, is in maintenance mode. Don’t start new projects with it. If you already know you want server rendering and file-based routing, npm create nuxt@latest scaffolds a Nuxt app instead, which Nuxt fundamentals covers.

What got generated

With Router and Pinia selected, the project looks roughly like this:

my-shop/
├── index.html          ← the real entry point
├── package.json
├── vite.config.js
├── jsconfig.json       ← (tsconfig.json with TypeScript)
├── public/             ← copied to the build as-is (favicon.ico)
└── src/
    ├── main.js         ← creates and mounts the app
    ├── App.vue         ← the root component
    ├── assets/         ← CSS and images processed by Vite
    ├── components/     ← reusable components
    ├── views/          ← page-level components, one per route
    ├── router/index.js ← route table (Router only)
    └── stores/         ← Pinia stores (Pinia only)

public/ versus src/assets/ trips people up. Files in public/ are served untouched at the site root, so /favicon.ico stays /favicon.ico. Files under src/assets/ go through Vite: imported images get hashed filenames for caching, and CSS is bundled.

How the app boots

Unlike older webpack setups, Vite treats index.html as source code, not a template buried in a config folder. It sits at the project root and is tiny:

<body>
  <div id="app"></div>
  <script type="module" src="/src/main.js"></script>
</body>

The empty div is where Vue will render. The module script loads main.js:

import "./assets/main.css";

import { createApp } from "vue";
import { createPinia } from "pinia";

import App from "./App.vue";
import router from "./router";

const app = createApp(App);

app.use(createPinia());
app.use(router);

app.mount("#app");

Three steps: createApp(App) creates an application instance with App.vue as its root component; app.use(...) installs plugins (here the store and the router, each covered in its own lesson); app.mount("#app") renders the root component into the element matching that selector. Everything you see on screen descends from App.vue. Importing a .vue file from JavaScript only works because of the build tool, which is the next piece.

vite.config.js and the @ alias

import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";

export default defineConfig({
  plugins: [vue(), vueDevTools()],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
});

@vitejs/plugin-vue is what teaches Vite to compile .vue single-file components, which the next lesson opens up. vite-plugin-vue-devtools injects Vue DevTools into the page during development. The @ alias maps to src/, so a deeply nested component can write import ProductCard from "@/components/ProductCard.vue" instead of counting ../../../.

The three scripts

package.json defines the commands you will use daily:

Command What it does
npm run dev Starts Vite’s dev server. Files are compiled on request, and editing a component hot-swaps it in the page while keeping its state where possible.
npm run build Produces the production bundle in dist/: minified, tree-shaken, with hashed filenames.
npm run preview Serves dist/ locally so you can check the real build before deploying.

The dev server is not what you deploy. dist/ is a folder of static files that any static host can serve, which deploying a frontend app covers.

Editor and devtools

Two tools make Vue much easier to work with:

  • Vue (Official), the VS Code extension formerly known as Volar, provides syntax highlighting, autocompletion, and type-checking inside templates. It replaces the old Vetur extension, which targeted Vue 2.
  • Vue DevTools shows the component tree, each component’s state, emitted events, router state, and Pinia stores. It’s available as a browser extension and, through the Vite plugin above, as an overlay in the page. Debugging with Vue DevTools covers it properly.

What the video covers

The embed starts at the “Installing Vue Locally” chapter of freeCodeCamp’s Scrimba-produced Vue course from November 2025. Over about nine minutes it runs create-vue, walks through the prompts, and tours the generated project’s anatomy, which is exactly this lesson’s ground. The course carries on into components after the “Section 1 Recap” chapter; stop there and continue with the single-file components lesson. Its prompt list may differ slightly from the one above as create-vue evolves, but the generated structure and boot sequence are the same. The Syntax crash course in the links below shows the same setup at a faster pace, including Vue DevTools.

Check yourself

  1. 01In a create-vue project, what is the chain of files that runs from the browser requesting the page to your App.vue appearing on screen?

    The browser loads `index.html`, which contains an empty `<div id="app">` and a module script pointing at `/src/main.js`. `main.js` imports `createApp` from `vue` and the root component from `./App.vue` (which Vite compiles through `@vitejs/plugin-vue`), creates an application instance, and calls `.mount('#app')`, which renders App and its children into that div.

  2. 02Why does the create-vue README insist on `npm create vue@latest` rather than `npm create vue`?

    Without the `@latest` tag npm may reuse a cached, outdated version of the create-vue package, so you would scaffold a project with old defaults and old dependency versions. Pinning `@latest` forces npm to fetch the current release.

  3. 03What is the difference between `npm run dev` and `npm run build` followed by `npm run preview`?

    `npm run dev` runs Vite's development server, which transforms files on request and supports hot module replacement; it is fast to start but not what users receive. `npm run build` bundles and minifies the app into static files in `dist/`, and `npm run preview` serves that production output locally so you can check it before deploying.

  4. 04You chose Vue Router and Pinia when scaffolding. Which extra pieces should you expect in the generated project?

    A `src/router/` folder with a router instance and route definitions, a `src/views/` folder with page-level components, a `src/stores/` folder with an example Pinia store, and a `main.js` that calls `app.use(createPinia())` and `app.use(router)` before mounting.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark