简体   繁体   English

react-icons 使用 webpack 解决错误

[英]react-icons resolve error with webpack

I saw the other question about react-icons not loading in webpack but the error I'm getting is a bit different and I have no idea how to fix it.我看到另一个关于react-icons未加载到 webpack 的问题,但我得到的错误有点不同,我不知道如何修复它。

I'm trying to use react-icons with webpack but I'm getting the following error:我正在尝试将 react-icons 与 webpack 一起使用,但出现以下错误:

ERROR in ./components/line-item.jsx Module not found: Error: Cannot resolve module 'react-icons' in public/map/components @ ./components/line-item.jsx 7:18-40 ./components/line-item.jsx 中的错误未找到模块:错误:无法解析 public/map/components @ ./components/line-item.jsx 中的模块“react-icons” 7:18-40

Here is my webpack setup:这是我的 webpack 设置:

var path = require('path');
var webpack = require('webpack');

var config = {
    iconPath: 'node_modules/react-icons'
};

module.exports = {
    entry: './main.js',
    output: {path: __dirname, filename: 'bundle.js'},
    module: {
        loaders: [
          {
            test: /.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
                presets: ['es2015', 'react']
            }
          },
          {
            test: /react-icons\/(.)*(.js)$/,
            loader: 'babel',
            include: config.iconPath
          },
          {
            test: /\.scss/,
            loader: 'style!css!sass'
          }
      ]
   }
};

Here is where I'm trying to import the react-icons in my line-item.jsx这是我尝试在 line-item.jsx 中导入 react-icons 的地方

import React from 'react';
import FaBeer from 'react-icons';

var LineItem = React.createClass({
})

module.exports = LineItem;

I'm brand new to webpack and just learning as I'm going but any help would be much appreciated.我是 webpack 的新手,我正在学习,但任何帮助将不胜感激。

EDIT: I changed the import to编辑:我将导入更改为

import FaBeer from 'react-icons/fa/beer';

and now getting a different error that I do believe is webpack related现在得到一个不同的错误,我相信它与 webpack 相关

ERROR in ./~/react-icons/fa/beer.js Module parse failed: /Users/oyachinskiy/Documents/ichnaea-root/web-reporting/public/map/node_modules/react-icons/fa/beer.js Unexpected token (8:12) You may need an appropriate loader to handle this file type.错误在 ./~/react-icons/fa/beer.js 模块解析失败:/Users/oyachinskiy/Documents/ichnaea-root/web-reporting/public/map/node_modules/react-icons/fa/beer.js Unexpected token (8:12) 您可能需要一个合适的加载器来处理这种文件类型。

Thanks!谢谢!

Try changing your import from:尝试更改您的导入:

import FaBeer from 'react-icons/fa/beer';

To:至:

import FaBeer from 'react-icons/lib/fa/beer';

That resolved the "Module parse failed" problem you're describing for me.这解决了您为我描述的“模块解析失败”问题。

By default babel ignores the node_modules directory.默认情况下 babel 会忽略 node_modules 目录。 Unless you change that setting, you need to import the icons from the lib directory.除非您更改该设置,否则您需要从 lib 目录导入图标。

See this GitHub issue for more details.有关更多详细信息,请参阅此 GitHub 问题

To import one icon:导入一个图标:

import FaBeer from 'react-icons/lib/fa/beer'

To import multiple icons:要导入多个图标:

import { MdCancel, MdChat, MdCheck } from 'react-icons/lib/md'

If you have run npm install react-icons then you should just be able to require it in your component.如果你已经运行npm install react-icons那么你应该能够在你的组件中使用它。 I don't believe you need to adjust the webpack configuration.我不相信你需要调整 webpack 配置。

EDIT--编辑--

The correct syntax is:正确的语法是:

import FaBeer from 'react-icons/fa/beer';

As per their NPM page .根据他们的NPM 页面

The right way to import is import FaBeer from 'react-icons/fa/beer' http://gorangajic.github.io/react-icons/index.html enter link description here .正确的导入方法是import FaBeer from 'react-icons/fa/beer' http://gorangajic.github.io/react-icons/index.html enter link description here

In the new versions of webpack it is necessary to put the suffix -loader在新版本的 webpack 中需要加上后缀-loader

{
  test: /react-icons\/(.)*(.js)$/,
  loader: 'babel-loader',
  query: {
    presets: ['es2015', 'react']
  }
}

I had the same issue before and it took me decades to solve it.我以前也遇到过同样的问题,我花了几十年才解决。

You should import it this way你应该这样导入

import {FaBeer} from 'react-icons/lib/fa/beer'从'react-icons/lib/fa/beer' 导入 {FaBeer}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM