Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack Sass - cannot resolve images

I am trying to compile my Sass via webpack. Compiling normal sass is fine but I get an error.

 Module not found: Error: Can't resolve '../img/twitter.svg' in '/Users/Steve/mywebsite/scss'
     @ ./~/css-loader!./~/sass-loader/lib/loader.js!./scss/main.scss 6:94501-94530

Is there a way to resolve this? Alternatively is there a way to set the level of the sass compiler to be less strict to just ignore certain errors

Below is my current config.

const path = require("path");
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  resolve: {
    alias: {
      masonry: "masonry-layout",
      isotope: "isotope-layout",
    },
  },

  entry: "./main.js",
  output: {
    path: path.resolve(__dirname, "./dist/dist2"),
    filename: "bundle.js",
  },

  module: {
    rules: [
      {
        test: /\.(png|jpg|svg)$/,
        include: path.join(__dirname, "/dist/img"),
        loader: "url-loader?limit=30000&name=images/[name].[ext]",
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: "babel-loader?presets[]=es2015",
      },

      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          use: ["css-loader", "sass-loader"],
        }),
      },

      {
        test: /\.vue$/,
        loader: "vue-loader",
        options: {
          loaders: {},
          // other vue-loader options go here
        },
      },
    ],
  },

  plugins: [
    // new webpack.optimize.UglifyJsPlugin(),
    new ExtractTextPlugin("ross.css"),
  ],
};
like image 809
LeBlaireau Avatar asked Mar 03 '17 09:03

LeBlaireau


People also ask

How to import Sass modules into Webpack?

Finally run webpack via your preferred method. Webpack provides an advanced mechanism to resolve files. The sass-loader uses Sass's custom importer feature to pass all queries to the Webpack resolving engine. Thus you can import your Sass modules from node_modules.

What is Sass-loader in Webpack?

This allows you to control the versions of all your dependencies, and to choose which Sass implementation to use." Essentially this loader has internal dependencies which require node-sass. sass-loader loads a Sass/SCSS file and compiles it to CSS. Now let's update the webpack.config.js.

How to use CSS in Webpack?

If you have a webpack configuration for production then you'll need a different configuration for using CSS. At first, install mini-css-extract-plugin npm i --save-dev mini-css-extract-plugin and now extract the miniCssExtractPlugin and replace the style-loader with MiniCssExtractPlugin.loader and add the MiniCssExtractPlugin in plugin.

How do I load a Sass/SCSS file?

Loads a Sass/SCSS file and compiles it to CSS. To begin, you'll need to install sass-loader: sass-loader requires you to install either Dart Sass or Node Sass on your own (more documentation can be found below). This allows you to control the versions of all your dependencies, and to choose which Sass implementation to use.


3 Answers

I know this is late, but for anyone looking for a workaround this error; In my case the image was loading perfectly in the template, however, Webpack was returning an error: Module not found: Error: Can't resolve './path/to/assets.png'

Fix/Workaround:

Add ?url=false to your css-loader, that will disable url handling by the css-loader :

...
{
  loader: "css-loader?url=false"
},
...
like image 87
Olamigoke Philip Avatar answered Oct 13 '22 05:10

Olamigoke Philip


I didn't have any luck with url-loader and file-loaderas suggested in the other answer. I was able to solve it using resolve-url-loader

module: {
  rules: [
    { // sass / scss loader for webpack
      test: /\.(sass|scss|svg|png|jpe?g)$/, //Make sure to allow all necessary file types here
      use: ExtractTextPlugin.extract({
        use: [
            {
              loader: 'css-loader',
              options: {
                importLoaders: 1,
                minimize: true,
                sourceMap: true
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                sourceMap: true
              }
            },
            {
              loader: "resolve-url-loader", //resolve-url-loader needs to come *BEFORE* sass-loader
              options: {
                sourceMap: true
              }
            },
            {
              loader: "sass-loader",
              options: {
                sourceMap: true
              }
            }
        ]
      })
    }
  ],
},
like image 34
dave Avatar answered Oct 13 '22 03:10

dave


This is a breaking change in css-loader 4.x (according to css-loader issue 1136).

like image 3
Marcel Studer Avatar answered Oct 13 '22 04:10

Marcel Studer