I am encountering difficulties with importing Node modules into my Vue.js project

Having some trouble importing a node module via NPM in a Vue.js single file component. No matter which module I try to install, it always throws an error saying

These dependencies were not found
. I'm following the installation instructions correctly (or so I believe), but there seems to be something missing.

For example :

Let's say I want to install the swiper.js module. I did the NPM installation in the correct directory, then added the following lines in the component where I intend to use it as per their documentation :

import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';

However, when I start my server, I encounter this error :

These dependencies were not found:

* swiper/css in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/ProductCarousel.vue?vue&type=script&lang=js&
* swiper/vue in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/ProductCarousel.vue?vue&type=script&lang=js&

To install them, you can run: npm install --save swiper/css swiper/vue

Edit :

Here is my package.json file:

{
  "name": "x",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "bootstrap": "^5.1.1",
    "bootstrap-vue": "^2.21.2",
    "core-js": "^3.6.5",
    "swiper": "^7.0.5",
    "vue": "^2.6.14"
  },
  "devDependencies": {
    "@fortawesome/fontawesome-free": "^5.15.4",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "vue-template-compiler": "^2.6.11",
    "webpack": "^4.46.0"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {}
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]
}

Answer №1

All seems well, except for the unnecessary import statement import 'swiper/css';.

You might want to consider deleting the node_modules directory and then re-running the command npm install

Answer №2

To ensure that a JavaScript script is available in Vue, you have two options: either import it in every component where you want to use it, or make it globally available by importing it in the main.js file.

For styling, you should import the styles in the app.vue file after opening the style tag to apply them globally across all components.

As an example using Bootstrap:

In main.js

import 'bootstrap'

In app.vue

<style lang="scss">  @import './node_modules/bootstrap/scss/bootstrap';</style>

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

Place form at the center of the Bootstrap Modal

My question is, how can I center the entire form, including the input fields and labels, within the modal? Here is also a helpful Fiddle: http://example.com, although the snippet provided should work as well. <script src="https://example.com/bootstr ...

While iterating through the material-ui rating component, the 'index' value remains constant at 0 within the 'onChange' function

For my e-commerce React.js web app, I want to show the rating value of each product. Using the JS map function, I was able to track which product was hovered by its index ('onChangeActive' is the handler for this). {products.map((product, index) ...

Obtain and install NPM packages in an offline environment

As a newcomer to npm, I am facing the challenge of working with NPM packages like express, express-generator, ejs, mysql, and more on a server that does not have Internet access. This means that the simple command npm install express will not be effective ...

Contrast between using "export { something }" and "export something" in JavaScript modules

Can you explain the difference between the following code snippets: import something from "../something"; export { something }; vs import something from "../something"; export something; I noticed in the react-is package from react, there is an export ...

Updating the @mui/x-data-grid table dynamically upon fetching new data

Seeking assistance regarding updating data in the DataGrid component from the @mui/x-data-grid module within a React application. Specifically, I am facing challenges in refreshing the table after retrieving data from an API using react-query. Despite succ ...

"You might require a suitable loader for managing this file format since there are currently no loaders set up to handle it."Webpack, vue.js

Having trouble with images in Vue.js Webpack Here's my Vue.js template: https://i.stack.imgur.com/h1zYx.png (ignore the red-line) This is my package.json: https://i.stack.imgur.com/v9rCD.png As well as webpack.config.js: https://i.stack.imgur.com ...

Positioning of the dropdown in Material UI AutoComplete menus

Is there a way to turn off autocomplete auto position? I would like the autocomplete options to always appear at the bottom. Check out this link for more information! ...

Using Express Router to call a dynamic route instead of the intended named route

I'm currently developing an API using Express.js, implementing routes and controllers. Within my api.js file, I have the following setup: const app = express() app.use('/clients', clients) Then, in my router/client.js file, I specify the e ...

Using jQuery to add emoticons to a div element

I am currently developing a small chat application and I would like to incorporate emojis into it. My goal is to allow users to click on an emoji, which will then appear in the text area where they type their message. When a user clicks on "select," I want ...

The loading of npm installed Firebase in Angular4 is not successful

I am currently facing an issue while trying to integrate npm installed Firebase with my Angular4 application. I have successfully installed the latest version of Firebase (version 4.1.1) using npm and verified that the installation was successful. Below is ...

Ways to reach component method via router

When making an Ajax request and displaying the data in a table component, I encounter an issue where I need to extract the clicked data for use in another Ajax request within a different component that is called through React-Router. The challenge lies in ...

Issue encountered: Unable to access X display - ERROR:browser_main_loop.cc(1400) - An unexpected error code was returned by the futex facility

I've been troubleshooting an electron project that involves a GUI and user input. In VSCode powershell terminal, I ran the following commands: npm install npm start After running these commands, error messages appeared in the terminal: Error Messa ...

How can I adjust the scale and position of my image textures in Three.js?

Is there a way to adjust the scale and position of my image textures? The dimensions of my image are 1024px x 1024px. let textureMap = THREE.ImageUtils.loadTexture( 'texture.png' ); https://i.sstatic.net/wKd6f.jpg ...

`Next.js project experiencing issues with scroll trigger functionality`

I've been working on a gsap animation with a scroll trigger, and while the animation itself functions fine, it's not triggering as expected. The AnimationEffect code involves using gsap and scrolltrigger to create the animation. The Hero section ...

How can I retrieve the OptionID value upon click?

How can I retrieve the value of OptionID when the Add button (.plus-link) is clicked? Each list item may contain a dropdown select menu or not. <ul> <li> <div class="menux"> <div class="text-block"> ...

Guide to attaching a page content button to a fixed header

I am currently developing a webpage with a sticky header, and I want to include an animated button that moves onto the header when scrolled past. You can check out my code snippet here: https://jsfiddle.net/ykL50pjf/ The specific ID for the button is #t ...

Looping through a set of API calls using JavaScript Web API

Currently, I am in the process of developing an application using angularjs and ionic. Within this app, I have an array containing IDs, and my objective is to retrieve their corresponding names. To achieve this, I attempted the following code snippet: var ...

Determine if a Node.js Express promise holds any data

I'm looking for assistance with my nodejs application that returns a promise. What I need help with is figuring out how to determine if the result of this promise contains data or if it's just an empty array. I attempted using Object.keys(result) ...

Is it necessary to close the browser for jQuery to reload an XML document?

I've successfully used jQuery's $.ajax to retrieve an xml value in my code. However, I'm facing an issue where any changes made to the xml document are not reflected upon a browser refresh. Specifically, the new saved xml value xmlImagePath ...

Using HTML and CSS to create interactive icons that change color when clicked, similar to how a link behaves

Have you ever wondered if there's a way to make an icon act like a link when clicked, just like regular text links turning purple? And not just the last one clicked, but every single icon that gets clicked. Trying to use the :visited pseudo was unsucc ...