^20.19.0 || >=22.12.0In this section we will introduce how to scaffold a Vue Single Page Application on your local machine. The created project will be using a build setup based on Vite and allow us to use Vue Single-File Components (SFCs).
Make sure you have an up-to-date version of Node.js installed and your current working directory is the one where you intend to create a project. Run the following command in your command line (without the $ sign):
::: code-group
$ npm create vue@latest$ pnpm create vue@latest# For Yarn (v1+)
$ yarn create vue
# For Yarn Modern (v2+)
$ yarn create vue@latest
# For Yarn ^v4.11
$ yarn dlx create-vue@latest$ bun create vue@latest:::
This command will install and execute create-vue, the official Vue project scaffolding tool. You will be presented with prompts for several optional features such as TypeScript and testing support:
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">{{ message }}</div>
<script>
const { createApp, ref } = Vue
createApp({
setup() {
const message = ref('Hello vue!')
return {
message
}
}
}).mount('#app')
</script><script setup> syntax, which requires build tools. If you intend to use Composition API without a build step, consult the usage of the setup() option.Throughout the rest of the documentation, we will be primarily using ES modules syntax. Most modern browsers now support ES modules natively, so we can use Vue from a CDN via native ES modules like this:
<div id="app">{{ message }}</div>
<script type="module">
createApp({
data() {
return {
message: 'Hello Vue!'
}
}
}).mount('#app')
</script><div id="app">{{ message }}</div>
<script type="module">
createApp({
setup() {
const message = ref('Hello Vue!')
return {
message
}
}
}).mount('#app')
</script>Notice that we are using <script type="module">, and the imported CDN URL is pointing to the ES modules build of Vue instead.
In the above example, we are importing from the full CDN URL, but in the rest of the documentation you will see code like this:
We can teach the browser where to locate the vue import by using Import Maps:
<script type="importmap">
{
"imports": {
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
}
}
</script>
<div id="app">{{ message }}</div>
<script type="module">
createApp({
data() {
return {
message: 'Hello Vue!'
}
}
}).mount('#app')
</script><script type="importmap">
{
"imports": {
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
}
}
</script>
<div id="app">{{ message }}</div>
<script type="module">
createApp({
setup() {
const message = ref('Hello Vue!')
return {
message
}
}
}).mount('#app')
</script>You can also add entries for other dependencies to the import map - but make sure they point to the ES modules version of the library you intend to use.
vuejs/petite-vue that could better suit the context where jquery/jquery (in the past) or alpinejs/alpine (in the present) might be used instead.As we dive deeper into the guide, we may need to split our code into separate JavaScript files so that they are easier to manage. For example:
<div id="app"></div>
<script type="module">
createApp(MyComponent).mount('#app')
</script>export default {
data() {
return { count: 0 }
},
template: `<div>Count is: {{ count }}</div>`
}export default {
setup() {
const count = ref(0)
return { count }
},
template: `<div>Count is: {{ count }}</div>`
}If you directly open the above index.html in your browser, you will find that it throws an error because ES modules cannot work over the file:// protocol, which is the protocol the browser uses when you open a local file.
Due to security reasons, ES modules can only work over the http:// protocol, which is what the browsers use when opening pages on the web. In order for ES modules to work on our local machine, we need to serve the index.html over the http:// protocol, with a local HTTP server.
To start a local HTTP server, first make sure you have Node.js installed, then run npx serve from the command line in the same directory where your HTML file is. You can also use any other HTTP server that can serve static files with the correct MIME types.
You may have noticed that the imported component's template is inlined as a JavaScript string. If you are using VS Code, you can install the es6-string-html extension and prefix the strings with a /*html*/ comment to get syntax highlighting for them.
If you skipped the Introduction, we strongly recommend reading it before moving on to the rest of the documentation.