Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 87 ยท Getting Started

Ways to Use Vue

Compare the three ways to use Vue 3 (CDN script tag, import maps, and a Vite project from create-vue with single-file components) and pick the right one.

Vue does not force a toolchain on you. The same framework runs from a single script tag on a static page or inside a large project with a build step, TypeScript, tests and hundreds of components. Choosing between those setups is the first real decision in a Vue project, and it is easier than it sounds once you know what each one gives you and costs you.

This lesson walks through the three common setups: a classic script tag, ES modules with an import map, and a build-tool project created with create-vue. You can run the first two right here; the third is shown read-only, because it needs Node.js and npm on your own machine.

Option 1: a script tag (the global build)

The simplest way is to load Vue with an ordinary script tag. The "global build" exposes one global variable called Vue:

Vue from a classic script tag

This works in every browser, needs no tooling and is great for sprinkling interactivity on a page that a server (for example a Laravel or WordPress site) already rendered. The downsides: all your code lives in script tags or big files, there are no import statements between your own files, and there are no single-file components.

Option 2: ES modules and an import map

Modern browsers understand import natively. An import map is a small piece of JSON that tells the browser where a bare name such as "vue" lives. After that, your code can write import { createApp } from "vue", exactly as it would in a build tool project:

Import map with a bare module name

Import maps are a good middle ground: no installation, real modules, and your project files can import each other with relative paths. This is the closest you can get to a build-tool project without one. The limits are speed (every module is a separate request, so it is not meant for big production sites) and, again, no .vue files.

Option 3: a build tool and single-file components

For anything bigger than a widget, most teams use a project created with create-vue, the official scaffolding tool. It uses Vite, a development server and bundler. Vite serves your files instantly while you work and produces small, optimized files for production. You need Node.js installed. These commands are read-only here, because the site cannot run a terminal:

Terminal
npm create vue@latest

The tool asks a few questions: the project name, and then whether you want TypeScript, JSX, Vue Router, Pinia, Vitest, an end-to-end test tool, ESLint and Prettier. Each "Yes" adds the package and a starter file. Then:

Terminal
cd my-vue-app
npm install
npm run dev

npm run dev starts the Vite development server (usually at http://localhost:5173) with hot reload: when you save a file, the browser updates without losing the app's state. npm run build writes the optimized site into a dist folder that you can upload to any static host.

The generated project looks like this:

TEXT
my-vue-app/
  index.html          page shell with <div id="app"></div>
  package.json        dependencies and scripts
  vite.config.js      build configuration
  public/             files copied as they are (favicon, ...)
  src/
    main.js           creates and mounts the app
    App.vue           the root component
    components/       your own components

And src/main.js is the same two-step story from the previous lesson:

JavaScript
import { createApp } from "vue";
import App from "./App.vue";

createApp(App).mount("#app");

The component is now a single-file component (SFC): a .vue file with a template, a script and styles together. A build step compiles it, which is why the template strings of the examples on this site are not used in such projects. You will see exactly how an SFC works in Single-File Components.

flowchart TD
    Q{"What are you building?"}
    Q -- "A widget on an existing page" --> A["Script tag or import map"]
    Q -- "An app with many components" --> B["Vite project from create-vue<br/>single-file components"]
    Q -- "SEO-critical or server-rendered site" --> C["Nuxt, built on Vue"]

Start with the lightest setup that meets the need. You can move up later without relearning Vue.

Reading an older project: Vue CLI, vue-loader and webpack

Before Vite, the standard way to start a Vue project was Vue CLI (npm install -g @vue/cli, then vue create my-app). It generated a project built on webpack, a general-purpose bundler, and used vue-loader, the webpack plugin that compiles .vue files. Vue CLI is now in maintenance mode: it still works and many companies still run it, but the Vue team recommends create-vue and Vite for new work. You will likely meet a Vue CLI project when you join a team or take over an old app, so it helps to read one quickly.

A typical Vue CLI project looks like this:

TEXT
my-app/
  public/index.html     page shell, with template tags such as <%= BASE_URL %>
  src/main.js           creates and mounts the app
  src/App.vue           root component
  babel.config.js       transpiling settings
  vue.config.js         optional webpack and dev-server settings
  .env, .env.production environment variables (VUE_APP_ prefix)
  package.json          scripts: "serve" and "build" (vue-cli-service)

Running npm run serve starts a webpack dev server with hot reload; npm run build writes a dist folder. Configuration goes through vue.config.js, which can also reach into the webpack configuration with configureWebpack or chainWebpack. If you see those names, you are looking at webpack tweaks that have no direct twin in Vite.

If the project is on Vue 2, its main.js creates the app with a constructor instead of createApp:

JavaScript
// Vue 2 with Vue CLI
import Vue from "vue";
import App from "./App.vue";

new Vue({
  render: (h) => h(App),
}).$mount("#app");
JavaScript
// Vue 3 with create-vue (Vite)
import { createApp } from "vue";
import App from "./App.vue";

createApp(App).mount("#app");

Here is how the old pieces map to the modern setup:

Vue CLI / webpack create-vue / Vite
vue create npm create vue@latest
vue-cli-service serve / build vite / vite build (scripts dev and build)
vue-loader @vitejs/plugin-vue
vue.config.js vite.config.js
public/index.html template index.html at the project root, with a <script type="module" src="/src/main.js"> tag
process.env.VUE_APP_API_URL import.meta.env.VITE_API_URL (see static assets and env variables)
require("./a.png"), require.context(...) import url from "./a.png", import.meta.glob(...)
~ prefix in CSS imports (~bootstrap/...) A normal package path, no ~
@ alias for src/ The same @ alias, set in vite.config.js
Babel and browserslist polyfills Modern-browser build target by default; use @vitejs/plugin-legacy for old browsers

A few practical tips when you open such a project. Check the Vue version in package.json first ("vue": "^2.x" or "^3.x"), because it decides which guides apply. Run npm ci rather than npm install to get the exact locked versions. Do not rewrite the build tool and the Vue version at the same time: first move to Vue 3 (with the migration build, see Migrating from Vue 2), and only then, if the build is slow or painful, switch from webpack to Vite. Your components and your .vue files mostly survive both steps, because the build tool and the framework are separate concerns.

What the site's runner supports

The live examples on this site use Option 2 style imports straight from a CDN URL (or an import map, as above). They support everything in Vue's core: templates, reactivity, components, directives, transitions and built-in components. They do not support .vue files, Vue Router, Pinia, or any other npm package, because there is no build step. Whenever a lesson needs one of those, you get a read-only block of real code and a runnable stand-in made from plain Vue.

Template string or single-file component?

Template string (CDN) Single-file component (build tool)
Setup None Node.js, npm, Vite
Template compiled In the browser, when the page loads Ahead of time, in the build
Download size Larger: includes the compiler Smaller: only the runtime
Styles A separate <style> element <style scoped> per component
Editor support Strings: no checking Full syntax highlighting and checking
Best for Learning, prototypes, widgets Real applications

Everything you learn about reactivity, directives and components is identical in both. Only the packaging changes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does an import map let you do?

    An import map is JSON that tells the browser where a bare module specifier lives. It is how code can use import from 'vue' without a build tool.
  2. 2. Which command creates an official Vue project with Vite?

    npm create vue@latest runs create-vue, the official scaffolding tool, which sets up a Vite-based project.
  3. 3. Why are template strings not used in a normal create-vue project?

    The build compiles each SFC's template into a render function, so the smaller runtime-only build of Vue can be shipped without the compiler.
  4. 4. You only need one interactive widget on a page that your server already renders. Which setup is the best fit?

    Vue is progressive. A script tag is enough for a widget, and you can move to a build tool if the project grows.

Summary

  • Vue works as a plain script tag (the global Vue object), as ES modules with an import map, or in a Vite project with single-file components.
  • A script tag or import map needs no tooling and is ideal for learning, prototypes and widgets on existing pages.
  • npm create vue@latest scaffolds a Vite project with optional router, Pinia, tests and linting; npm run dev starts it and npm run build creates the production files.
  • Build-tool projects compile templates ahead of time, so the shipped code is smaller; template strings are for the CDN setups.
  • The live examples support all of core Vue, but not .vue files, Vue Router or Pinia, which appear read-only with runnable stand-ins.

Next: Single-File Components