How can I include npm package js and css files in Vue CLI with webpack?

I am currently attempting to integrate an npm package into my vue-cli development site. The instructions provided by the package website are as follows:

https://i.stack.imgur.com/R2Inl.png

The instructions state that both the css and js files need to be included within the code. How should I proceed with this?

My assumption is that the css file can be included like this in App.vue:

<style>
  @import '../node_modules/swiper/dist/css/swiper.min.css';
....
</style>

Is this correct? And how do I go about including the .js file? Should it be done inside App.vue or main.js? I am unsure of the syntax for either case.

This is the content of my index.html file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="build/swiper.min.css">
  </head>
  <body>
    <div id="app"></div>
    <script src="build/swiper.min.js"></script>
  </body>
</html>

Answer №1

To include these files in your project, you can add them to the build/index.html file along with other common js and css elements.

<head> 
....
 <link rel="stylesheet" href="path_to_css/idangerous.swiper.css">
 <script defer src="path_to_js/idangerous.swiper-2.x.min.js"></script> 
.... 
</head>

Answer №2

After some tinkering, I found a solution that seems to be effective - within my main.js script, I have included the necessary js and css files in the following manner:

import Swiper from 'swiper'
import 'swiper/dist/css/swiper.min.css'

Based on my observations, it appears that this method accurately incorporates the npm package into the project for seamless utilization during both development and production stages.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for defining header and parameters when using route.get in Node.js

Looking to add custom headers and parameters when using route.get in Node.js? I am trying to set a specific value for the header and pass parameter values in the API URL. router.get("/getdata", async (req, res) => { // Set custom header re ...

What is the method for executing a specific task using grunt.registerTask()?

My grunt tasks are registered like this - grunt.registerTask('regenerateSources', ['clean:local', 'bower', 'uglify']); When I run grunt regenerateSources, it executes the clean, bower and uglify tasks. But is there ...

Having trouble setting up Node.js on a Mac computer

Upon completing the regular installation on Mac, I am unable to find Node in the terminal. Even after defining the PATH with the line: export PATH=/usr/local/bin: $PATH, it still does not locate Node or npm in the terminal. ...

Wait for the completion of the HTTP request before proceeding with the loop in Node.js

Hey there! I'm a beginner in the world of javascript and I could use some assistance. Below is the code that's been giving me trouble: let url = "https://drive.google.com/uc?export=download&id=driveid"; while(true) { let check = fal ...

NodeJS speech recognition technology is revolutionizing the way we interact

Currently, I am developing a tool that allows me to access all my notifications by connecting to various APIs. While the tool is functioning well, I am interested in adding voice commands for performing actions. For example, when the software notifies "O ...

What is the best way to save static information (such as country codes) within a Vue.js application?

After researching the most common best practices for handling this issue, I have found no clear answer. How should static content like country codes or arrays of categories be stored in a vue.js app? Storing it in my .env file as an environment variable se ...

Is it possible for a mongoose to retrieve all the distinct values for a specific field?

In my MongoDB database, I am storing information about the various rooms available in a hotel. These rooms come in different types such as singles, doubles, and more. My goal is to allow the user to customize their own room type. Is there a way to retrieve ...

What is the reason for my Node.js server mirroring every input I send to it using nc?

As a Node.js beginner, I am delving into learning by constructing a server that listens to a Unix-domain socket. To test my server, I am utilizing 'nc' as a client with the command 'nc -U /var/run/ipc.sock'. Progress is being made, but ...

Acquire a Vue component on a separate page by clicking a button on another Vue component or an HTML button click

Is there a way to dynamically load a Vue component on a different page when an HTML button is clicked? -HTML button <a class="contentButton ml-n5" id="addCategoryPrice">Add Price<i class="fa fa-plus-circle"></i ...

Error: The operation 'join' cannot be performed on an undefined value within Fast2sms

I am encountering issues while attempting to send SMS using fast2sms in Node.js. The error message reads as follows: TypeError: Cannot read property 'join' of undefined at Object.sendMessage (C:\Users\user\Desktop\node_module ...

The function Router.use is looking for a middleware function, but instead received an object in node.js /

I encountered an issue while trying to setup routing in my application. Whenever I attempt to initialize a route using app.use() from my routes directory, I receive an error stating that Router.use() requires a middleware function but received an Object in ...

Am I implementing the Vue.JS onChange function correctly?

After selecting an option from mod_accs_Role_ID, how can I display the corresponding data stored in the database based on that selection? For example, if I select 1 in mod_accs_Role_ID, then the role_Name 'Hello' should be displayed. <div clas ...

When attempting to execute a query in Node using oracledb, an error with the code 'npm ERR! errno 3221225477' occurred

Encountered the following error message in the command line: npm ERR! code ELIFECYCLE npm ERR! errno 3221225477 npm ERR! [email protected] start: `node ./bin/www` npm ERR! Exit status 3221225477 npm ERR! npm ERR! Failed at the [email protected] start sc ...

Contrasts in the immutability strategies of Vuex and Redux

After exploring Vuex and noticing how simple it is to mutate states with mutation handlers using basic assignment, I am currently delving into redux. I have come to realize that redux emphasizes immutability, which can make coding a bit more verbose. This ...

Unleashing the power of JavaScript: Sharing arrays and data structures effortlessly

Currently, I am utilizing HTML & JavaScript on the client side and NodeJs for the server side of my project. Incorporated in my form are multiple radio buttons. When the user clicks on the submit button, my intention is to post the results to the server. ...

What are the steps for integrating vue-i18n into a Vue web component?

As I work on developing a Vue web component with vue-cli 3 and the --target wc option, I have encountered an issue. The vue-i18n plugin that I need to use requires certain options to be passed to the main Vue instance as shown below: new Vue({ i18n: n ...

Combining arrays of objects in JavaScript

I am currently working on combining different arrays: const info1 = {id: 1} const info2 = {id: 2} const info3 = {id: 3} const array1 = [info1, info2] const array2 = [info1, info3] const array3 = [info2, info3] const union = [...new Set([...array1, ...arr ...

Create a new file in the user's home directory whenever the `npm install` command is

When I install a foo module, as a developer, I aim to create a ~/.foo.json file in the user's home directory (~/). To achieve this, I added the following to my package.json: { ... "scripts": { "preinstall": "./installation/preinstall.js" ...

Attempting to run the npm install command led to encountering a SyntaxError with an

Recently, I made some alterations to my npm configuration and ever since then, I have been consistently encountering the same error whenever I try to install various packages. It seems that due to a personal mistake in changing the npm settings, I am now u ...

Guide on verifying the presence of an alert with nodejs webdriver (wd)

I am currently facing a challenge when writing a test where I need to verify the existence of an alert, check its text if it is present, and then accept it. Although I have researched on platforms like Stack Overflow for solutions such as checking for ale ...