当我尝试使用webpack 构建SASS文件时,出现以下错误:
找不到模块:错误:无法解析模块'file-loader'
请注意,此问题仅在我尝试使用相对路径加载背景图像时发生.
这项工作很好:
background:url(http://localhost:8080/images/magnifier.png);
Run Code Online (Sandbox Code Playgroud)
这导致了问题:
background:url(../images/magnifier.png);
Run Code Online (Sandbox Code Playgroud)
这是我的项目结构
这是我的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']
}
};
Run Code Online (Sandbox Code Playgroud)
Mou*_*wi7 46
正如@silvenon在评论中所说:
你有安装文件加载器吗?
是的文件加载器已安装但已损坏,我的问题已通过重新安装解决.
npm install --save-dev file-loader
如果您使用定义的配置,您可能会遇到非常类似的问题。正如文档所述,如果您尝试加载的资源超过此限制,则将用作后备。因此,如果没有安装,会提示错误。要修复此错误,请将此限制设置为更大的值或根本不定义它。url-loaderlimitfile-loaderfile-loader
{
test: /\.(jpg|png|svg)$/,
use: {
loader: 'url-loader',
options: {
limit: 50000, // make sure this number is big enough to load your resource, or do not define it at all.
}
}
}
Run Code Online (Sandbox Code Playgroud)