【问题标题】:Node modules won't import in my Vue.js project节点模块不会导入到我的 Vue.js 项目中
【发布时间】:2021-11-10 09:30:46
【问题描述】:

我正在尝试在 Vue.js 单文件组件中导入通过 NPM 安装的节点模块。每次,无论模块是什么,它都不会工作并抛出诸如These depedencies were not found 之类的错误。我按照正确的安装说明进行操作(至少我认为),但我想我遗漏了一些东西。

例子:

尝试安装swiper.js 模块。 我在正确的文件夹中安装了 NPM,然后在我想使用它的组件中添加了以下几行,例如他们网站上的解释:

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

当我启动我的服务器时,我收到了这个错误:

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

编辑:

这是我的 package.json 文件:

{
  "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"
  ]
}

【问题讨论】:

  • 你能粘贴你的package.json吗?
  • @tauzN 已添加!
  • 一切似乎都是正确的。我建议删除您的 node_modules 文件夹并再次运行 npm i

标签: javascript vue.js npm webpack


【解决方案1】:

在 vue 中要使 js 脚本可用,你应该将它导入到每个你想在 . 或者你可以通过在 main.js 中导入它来使其全局可用

对于样式,您应该在打开样式标记后将其导入 app.vue 以使其对每个组件样式都具有全局性。

如果我们可以以引导程序为例。 在 main.js 中

import 'bootstrap'

在 app.vue 中

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

【讨论】:

    【解决方案2】:

    一切看起来都很好,除了 import 'swiper/css'; 不需要导入。

    您可以尝试删除您的 node_modules 文件夹并再次运行 npm install

    【讨论】:

    • 记得删除 .lock 文件。
    • @wittgenstein 我实际上建议不要这样做,因为所有间接依赖项都可能发生变化并可能搞砸你的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2017-05-19
    • 2018-01-08
    • 2021-12-25
    • 1970-01-01
    • 2018-07-10
    相关资源
    最近更新 更多