How to Add External JS Scripts to VueJS Components?
Last Updated :
07 May, 2024
VueJS is a popular front-end JavaScript framework that allows us to develop single-page web applications with the help of components. We can also add external JS scripts in the framework to integrate any 3rd party web services into our application.
Below are the approaches to add external JS scripts to VueJS components:
Steps to create the VueJS app:
Step 1: Create an app
We will create a vue app with the below commands. We will then use this app to use video.js library.
vue create sample-vue
This will create a sample vue application with the below project structure.
Step 2: Go to the folder
cd sample-vue
Project structure:

Using VueJS lifecycle hooks
In this approach, we will use the VueJS lifecycle hooks to create a script HTML node, and then insert that script node into the document itself.
Example: In this example, we will insert the script in the mounted lifecycle hook of VueJS.
HTML
//App.vue
<template>
<div>
<h1>Hello Vue.js!</h1>
</div>
</template>
<script>
export default {
mounted() {
const script = document.createElement("script");
script.src =
"https://cdn.jsdelivr.net/npm/[email protected]/src/index.min.js";
document.body.appendChild(script);
},
};
</script>
Steps to run the server:
Now, we will run our web application by running the below command -
pnpm serve
Output:

Import in a specific Vue component
In this approach, we will install the NPM package corresponding the script, and then use that package in our components. we will use the installed package in the App.vue file, and just call the method "helloWorld", which is provided the package itself, when the component gets mounted.
pnpm install hello-world-npm
Example: In this example, we will install an NPM package, and then import that package to use it in the component.
JavaScript
<template>
<div>
<h1>Hello Vue.js!</h1>
</div>
</template>
<script>
import { helloWorld } from "hello-world-npm";
export default {
mounted() {
console.log(helloWorld());
},
};
</script>
Steps to run the server:
Now, we will run our web application by running the below command -
pnpm serve
Output:

Similar Reads
How to Add Custom Fonts in VueJS ? Vue.js is a JavaScript framework used in building powerful and beautiful user interfaces. The key feature of Vue.js is its component-based architecture that allows the developers to create reusable and modular components. Custom fonts can bring uniqueness and visual appeal to your Vue.js application
2 min read
How to Update Parent Data from Child Component in VueJS? In Vue.js, communication between components is essential for building dynamic applications. One common scenario is updating the parent component's data from a child component. we will cover several ways to achieve this, including custom events, and v-model. Updating parent data from a child componen
3 min read
How to Create Functional Components using Vue Loader ? Vue.js is a JavaScript framework used in building powerful and beautiful user interfaces. The key feature of Vue.js is its component-based architecture that allows the developers to create reusable and modular components. Functional components, in particular, provide a lightweight and efficient way
3 min read
How to Pass Value from One Child Component to Another in VueJS ? Vue.js is a JavaScript framework used in building powerful and beautiful user interfaces. The key feature of Vue.js is its component-based architecture that allows the developers to create reusable and modular components. In this article, we will learn how to pass value from one child component to a
3 min read
Vue.js Passing Data to Child Components with Props Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
How to include an external JavaScript library to ReactJS ? JavaScript library is pre-written collection of code snippets, objects, and functions so that we can reuse the functions, objects, and code snippets to perform a common task. It is generally use as the scripts in the <script> tags. We can use different methods to include external JavaScript li
4 min read