我有一个使用webpack和一些文件加载器规则的React项目。我试图在我的React组件(JSX)中导入/要求一个图像:
<img src={require('../../assets/images/msg.png')} />
在macOS计算机中它会抛出此错误:
ERROR in ./src/components/Header/index.js
Module not found: Error: Can't resolve '../../assets/images/search.jpg' in '/Users/leonimurilo/Personal projects/4Petson/src/components/Header'
@ ./src/components/Header/index.js 165:56-97
@ ./src/components/App/index.js
@ ./src/routes.js
@ ./src/index.js
@ multi ./src/webpack-public-path webpack-hot-middleware/client?reload=true ./src/index.js
令人惊讶的是,当我尝试在Windows 7计算机上运行该项目时,它运行得非常好!
这是我的webpack模块对象:
module: {
rules: [
{test: /\.jsx?$/, exclude: /node_modules/, loaders: ['babel-loader']},
{test: /\.eot(\?v=\d+.\d+.\d+)?$/, loader: 'file-loader'},
{test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&mimetype=application/font-woff'},
{test: /\.[ot]tf(\?v=\d+.\d+.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=application/octet-stream'},
{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=10000&mimetype=image/svg+xml'},
{test: /\.(jpe?g|png|gif)$/i, loader: 'file-loader?name=[name].[ext]'},
{test: /\.ico$/, loader: 'file-loader?name=[name].[ext]'},
{test: /(\.css|\.scss|\.sass)$/, loaders: ['style-loader', 'css-loader?sourceMap', 'postcss-loader', 'sass-loader?sourceMap']}
]
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
即使版本相同,将文件加载器更新到最新版本也解决了这个问题。