Javascript is required
·
1 min read

Vue Tip: Use Vue Without Build Step

Vue Tip: Use Vue Without Build Step Image

Vue can be used as a standalone script file - no build step required!

Some typical use cases:

  • You don't want to add the complexity of a build step as your frontend logic is not very complex
  • You are using a backend framework that is rendering most of the HTML

The official docs describe Vue without a build step as "a more declarative replacement of jQuery".

To avoid the build step, we can load Vue directly from a CDN via the script tag:

index.html
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

In our example, we use unpkg as CDN to fetch the npm package. Of course, you can use any other CDN that serves npm packages or download the file and serve it yourself.

The CDN link loads a global build of Vue. This build exposes all top-level APIs on the global Vue object. Let's take a look at a working example:

index.html
1<!DOCTYPE html>
2<html lang="en">
3  <head>
4    <meta charset="UTF-8" />
5    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7    <title>Vue Standalone Demo</title>
8  </head>
9  <body>
10    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
11
12    <div id="app">{{ message }}</div>
13
14    <script>
15      const { createApp } = Vue
16
17      createApp({
18        data() {
19          return {
20            message: 'Hello Vue!',
21          }
22        },
23      }).mount('#app')
24    </script>
25  </body>
26</html>

I deployed this index.html to Netlify: Check it out

Notes on Production Use

When deploying to production you should make sure to use the production builds (dist files that end in .prod.js):

index.html
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>

Check Which dist file to use? for more details.