Webpack:无法解析模块“文件加载器”

上海社会科学院 CSS

卡卡西Near

2020-03-23

当我尝试使用webpack 构建SASS文件时,出现以下错误:

找不到模块:错误:无法解析模块“文件加载器”

请注意,仅当我尝试使用相对路径加载背景图像时,才会发生此问题。

这项工作很好:

  background:url(http://localhost:8080/images/magnifier.png);

这会导致问题:

   background:url(../images/magnifier.png);

这是我的项目结构

  • 图片
  • 样式
  • webpack.config.js

这是我的webpack文件:

var path = require('path');
module.exports = {
    entry: {
        build: [
            './scripts/app.jsx',
            'webpack-dev-server/client?http://localhost:8080',
            'webpack/hot/only-dev-server'
        ]
    },
    output: {
        path: path.join(__dirname, 'public'),
        publicPath: 'http://localhost:8080/',
        filename: 'public/[name].js'
    },
    module: {
        loaders: [
            {test: /\.jsx?$/, loaders: ['react-hot', 'babel?stage=0'], exclude: /node_modules/},
            {test: /\.scss$/, loaders: ['style', 'css', 'sass']},
            {test: /\.(png|jpg)$/, loader: 'file-loader'},
            {test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, loader: 'file-loader'}
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.scss', '.eot', '.ttf', '.svg', '.woff'],
        modulesDirectories: ['node_modules', 'scripts', 'images', 'fonts']
    }
};

第2592篇《Webpack:无法解析模块“文件加载器”》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

3个回答
逆天米亚 2020.03.23

我有完全相同的问题,以下内容已解决:

loader: require.resolve("file-loader") + "?name=../[path][name].[ext]"
村村 2020.03.23

谢谢-这是获得Bootstrap,d3js,Jquery,base64内嵌图像和我自己写得不好的JS以便与webpack一起使用的最后一块。

要回答上述问题以及解决
未找到有问题的模块的解决方案,请执行以下操作:错误:无法解析模块“ url”, 当时编译引导字体是

{ 
test: /glyphicons-halflings-regular\.(woff2|woff|svg|ttf|eot)$/,
loader:require.resolve("url-loader") + "?name=../[path][name].[ext]"
}

谢谢!

阿飞米亚 2020.03.23

正如@silvenon在评论中所说:

您是否安装了文件加载器?

是的,文件加载器已安装但已损坏,并且我的问题已通过重新安装解决。

npm install --save-dev file-loader

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android