Webpack - sass加载程序找不到图像 [英] Webpack - sass loader cannot find images

查看:1247
本文介绍了Webpack - sass加载程序找不到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sass loader doc说:如果你只是生成CSS而不传递给css-loader,您的Web根
所以我说的,我有 index.html 在我的项目根,然后我尝试从我的 scss 文件加载图像
现在我有2个错误:1)从 Chrome的控制台找不到模块./img/header.jpg\"。 2)从我的终端

The sass loader doc says: "If you're just generating CSS without passing it to the css-loader, it must be relative to your web root". So i did as it tells, I have my index.html in my project root, then I'm trying to load an image from my scss file. Now I have 2 errors: 1) from Chrome's console: Cannot find module "./img/header.jpg". 2) from my terminal:

ERROR in ./~/css-loader!./~/sass-loader!./~/resolve-url-loader!./public/css/header.scss
Module not found: Error: Cannot resolve 'file' or 'directory' ./img/header.jpg in C:\Web-Development\React\Portfolio\public\css
 @ ./~/css-loader!./~/sass-loader!./~/resolve-url-loader!./public/css/header.scss 6:64-91

webpack.config.js

module.exports = {
    entry: './main.jsx',
    output: {
        filename: './public/js/build/bundle.js'
    },
    module: {
        loaders: [
            {
              test: /\.jsx?$/,
              exclude: /(node_modules|bower_components)/,
              loader: 'babel',
              query: {
                  presets: ['react', 'es2015']
              }
          },
          {
              test: /\.scss$/,
              loaders: ["style", "css", "sass", "resolve-url"]
          },
          {
            test: /\.jpg$/,
            loader: "file?name=[path][name].[ext]"
          }
        ]
    }
};

如果我看到我的代码,我可以清楚地看到我的css住在 < head> ,因此我已将图片的路径指向我的根目录,如文件所述,但仍无法修复。

If I see my code, I can clearly see that my css lives inside <head> so I've pointed my image's path to my root, as documentation says, but still can't fix it.

UPDATE:
我安装了 file-loader 并按照说明操作,现在我在控制台中遇到此错误: GET http:// localhost:3000 / public / img / header.jpg 404(未找到) - jquery.js:9119

UPDATE: I've installed file-loader and followed the instructions, now I get this error in console: GET http://localhost:3000/public/img/header.jpg 404 (Not Found) - jquery.js:9119

推荐答案

为了解决这个问题,我已上传此 webpack extremly basic配置启动我的项目,我希望它可以帮助每个有这个问题的人。当然欢迎提出建议。

To solve this problem I've uploaded this webpack extremly basic configuration to start my projects, I hope it can help everyone who had this problem. Suggestions are all welcome of course.

这篇关于Webpack - sass加载程序找不到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆