首页 > 解决方案 > VueJS 和 FontAwesome 导入 - 未找到模块:错误:无法解析 '@fortawesome/fontawesome-svg-core'

问题描述

Module not found: Error: Can't resolve '@fortawesome/fontawesome-svg-core' in '/project/src/plugins'

一直在尝试跟进本教程(https://blog.logrocket.com/full-guide-to-using-font-awesome-icons-in-vue-js-apps-5574c74d9b2d/)来升级我当前的fontawesome(4.0 .7) 到我项目的最新 5.0.12 版本,但不断收到此错误,很明显该项目没有找到该库。

我用了

npm install --save @fortawesome/fontawesome-svg-core 
npm install --save @fortawesome/free-solid-svg-icons 
npm install --save @fortawesome/vue-fontawesome

src/plugins/font-awesome.js

import Vue from 'vue'

import { library } from '@fortawesome/fontawesome-svg-core'
import { faCoffee } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

library.add(faCoffee)

Vue.component('font-awesome-icon', FontAwesomeIcon)

main.js

/* ============
 * Main File
 * ============
 *
 * Will initialize the application.
 */

import Vue from 'vue';

/* ============
 * Plugins
 * ============
 *
 * Import and bootstrap the plugins.
 */

import './plugins/vuex';
import './plugins/axios';
import { i18n } from './plugins/vue-i18n';
import { router } from './plugins/vue-router';
import './plugins/vuex-router-sync';
import './plugins/bootstrap';
import './plugins/font-awesome';
import './plugins/moment';
import './plugins/vuelidate';
import './plugins/scrollto';
import './plugins/filters';
import './plugins/casl';

// Theme css imports
import './assets/css/application.scss';


/* ============
 * Main App
 * ============
 *
 * Last but not least, we import the main application.
 */

import App from './App.vue';
import store from './store';

Vue.config.productionTip = false;

store.dispatch('auth/check');

/* eslint-disable no-new */
new Vue({
  /**
   * Bind the Vue instance to the HTML.
   */
  el: '#app',

  /**
   * The localization plugin.
   */
  i18n,

  /**
   * The router.
   */
  router,

  /**
   * The Vuex store.
   */
  store,

  /**
   * Will render the application.
   *
   * @param {Function} h Will create an element.
   */
  render: h => h(App),
});

其他插件工作正常,该文件夹实际上存在于 node_modules 上。我应该尝试其他任何方法来解决这个问题吗?

标签: vue.jsfont-awesomevue-cli

解决方案


我有同样的问题

webpack css-loader 不支持(scss 和 .css 捆绑)你需要 sass 来做

只需安装 sass-loader 和 node-sass(阅读下面的注释)

npm install -D sass-loader node-sass<----------------see below note before install--------------

网页包

module.exports = {
  module: {
    rules: [
        {
        test: /\.(sass|scss|css)$/,<-----------------------note placed scss /css---------
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'<--------------------this is you need----------------
        ]
      }
    ]
  },
  // plugin omitted
}

注意:人们在安装时会出错,npm install -d sass-loader但是 sass-loader 带来了最新版本的 sass 意味着您获得了 sass-loader:11.0.0 或更高版本,因此 webpack 4 不支持 webpack 5 以上版本 11 以上的最新 sass-loader.sass 加载器。

解决方案

你的 webpack 版本是 4,所以你只需要低于 sass loader 版本 10

npm install -D node-sass
npm install -D sass-loader@^10 <-----------importent step

我的网络包

const HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')

module.exports = {
    entry: './src/main.js',
    module: {
        rules: [{
                test: /\.js$/,
                use: 'babel-loader'
            },
            {
                test: /\.vue$/,
                use: 'vue-loader'
            },
            {
                test: /\.(sass|scss|css)$/,
                use: ['vue-style-loader', 'css-loader','sass-loader'],
              
            },
           
        ]
    },
    devServer: {
        open: true,
        hot: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
        }),
        new VueLoaderPlugin(),
        new webpack.HotModuleReplacementPlugin()
    ],

}

推荐阅读