0

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.

1 Answer 1

2

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:

Sign up to request clarification or add additional context in comments.

1 Comment

I believe you can just say import './main.css'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.