Webpack css-loader: "Module not found: Error: Can't resolve 'main.css' in ..."

Magnus

I am trying to include my css in the server hosted by webpack-dev-server. For that to happen, I apparently have to use style-loader and css-loader together, in order to bundle the css into the JavaScript.

I can't get it to work.

I follow the instructions here, yet I get the following error:

ERROR in ./src/index2.js

Module not found: Error: Can't resolve 'main.css' in C:\Users\magnusga\Downloads\Programming\TestPrograms\test\src'

@ ./src/index2.js 1:0-27

@ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index2.js

I know for certain that main.css is in the same folder as index2.js


My Settings

index2.js

import css from 'main.css';
// ...much more code

webpack.config.js

module.exports = {
    entry: {
        app: './src/index2.js'
    },
    devtool: 'inline-source-map',
    devServer: {
        contentBase: './dist'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                exclude: /node_modules/,
                use: ['style-loader', 'css-loader']
            }
        ],
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
            title: 'Development',
            template: 'src/index.html',
            inject: 'head'
        })
    ],
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    }
};

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index2.js",
  "dependencies": {
    "rxjs": "^5.5.6"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "clean-webpack-plugin": "^0.1.17",
    "css-loader": "^0.28.11",
    "extract-text-webpack-plugin": "^3.0.2",
    "html-webpack-plugin": "^2.30.1",
    "postcss-loader": "^2.1.3",
    "sass-loader": "^6.0.7",
    "style-loader": "^0.20.3",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.11.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "buildb": "babel src --watch --out-dir built --copy-files",
    "watch": "webpack --watch",
    "start": "webpack-dev-server",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC"
}

One Fix

One fix is to use import css from './main.css'; instead of import css from 'main.css'; (note the ./ infront of the file name).

That does not feel right though, because the css-loader site shows that it should be the latter, not the former.

Is it a typo in the docs?

Thank you.

Daniel

It is not really a typo. If you import it like this:

import css from 'main.css';

Webpack thinks, that you want to import a module, and searches for this file under node_modules. This is necessary, when you for example installed the bootstrap package and want to import its css. So when your css file comes from a dependency, you import that dependency like this. But when you want to import a lokal file, always use relative paths.

So it must be: import css from './main.css';

Further Reading:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Loader for CSS / SASS in Webpack

Webpack not recognising css loader

Webpack css-loader not building

Sourcemaps with webpack css-loader

Webpack raw minified css loader

webpack css loader not work: no output css file

Webpack relative css url() with file-loader / css-loader

Webpack file-loader and images in CSS

webpack with css-loader - Parsing error: ';' expected

webpack css loader doesn't work

Webpack css-loader does not resolve aliases

webpack sass-loader not generating a css file

Webpack sass-loader Invalid CSS

What is the purpose of css-loader in webpack

Webpack CSS Loader Error: "Unknown word" on variables

Preact change webpack css loader config

Difference: Webpack css-loader and raw-loader

Webpack style-loader vs css-loader

Webpack extract-text-webpack-plugin and css-loader minification

Webpack css loader configuration outside of webpack.config?

Webpack: Why 'style-loader!css-loader!postcss-loader' doesn't process imported files?

Trying to use css-loader with webpack to minimize our css

Webpack css-loader not handling the css although the configuration appears to be alright

Load some CSS with style-loader and some CSS with to-string-loader in Webpack 2

Webpack style-loader vs mini-css-extract-plugin

Configuration for enabling hot style-loader in Webpack for syncing css

Why Webpack's sass-loader is minimizing CSS in production mode?

Error in CSS font loader when using webpack 2.2.0

How to configure webpack css loader in a vue-cli project

Webpack loader configuration to load css and sass files for React JS