简体   繁体   English

SCSS文件无法导入node_module

[英]SCSS file cannot import node_module

Tried @import "~react-icons" in an SCSS file. 在SCSS文件中尝试@import“〜react-icons”。

I get an error: Module build failed: @import "~react-icons"; 我收到一个错误:模块构建失败:@import“〜react-icons”; ^ File to import not found or unreadable: ~react-icons. ^找不到导入文件或不可读:〜反应图标。

This was an npm module that I installed and is on package-json. 这是我安装的npm模块,位于package-json上。

My scss code looks like this, the error is at the first line: 我的scss代码如下所示,错误出现在第一行:

@import "~react-icons";

input {
    width: 100%;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    background-color: white;
    padding: 12px 20px 12px 20px;
    margin-bottom: 20px;
}

My webpack configuration looks like this: 我的webpack配置如下所示:

const webpack = require('webpack');
const path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

let BUILD_DIR = path.resolve(__dirname, 'dist');
let APP_DIR = path.resolve(__dirname, 'src');

let config = {
    entry: path.join(APP_DIR, '/index.js'),
    output: {
        path: BUILD_DIR,
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.jsx?/,
                include: APP_DIR,
                loaders: ['babel-loader']
            },
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                  fallback: "style-loader",
                  use: ['css-loader', 'sass-loader'],
                  publicPath: "/dist"
                })
            },
            {
                test: /\.(ttf|eot|svg|gif|woff(2)?)(\?[a-z0-9]+)?(\?v=
                     [0-9]\.[0-9]\.[0-9])?$/,
                loader: 'file-loader',
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.jsx']
    },
    devServer: {
        contentBase: path.join(__dirname),
        // serve index.html in place of 404 responses to allow HTML5 
        // history
        historyApiFallback: true
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({
          title: 'Project',
          minify: {
            collapseWhitespace: true
          },
          hash: true,
          template: './index.html'
        }),
        new ExtractTextPlugin({
          filename: 'style.css',
          disable: false,
          allChunks: true
        })
    ]
};

module.exports = config;

There is not an scss file to import for react-icons . 没有要为react-icons导入的scss文件。 You have to import the icons in the file you want to use: 您必须将图标导入要使用的文件中:

import FaSearch from 'react-icons/lib/fa/search';

class Search extends React.Component {
    render() {
        return <FaSearch />
    }
}

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

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