24

I'm using css-loader and get following error:

ERROR in ./src/pages/home/index.js
Module not found: Error: Can't resolve 'css-loader' in '/Users/jian/Documents/sina/webpack-barbarian-test'
@ ./src/pages/home/index.js 2:0-20
@ multi ../webpack-barbarian/node_modules/webpack-dev-server/client?http://localhost ../webpack-barbarian/node_modules/webpack/hot/dev-server.js ./src/pages/home/index.js

./src/pages/home/index.js:

import $ from 'jQuery'
import './style.css'

$("#container").html('This is a test file, 1')

webpack.config.js:

{
    mode: 'development',
    entry: {
        home: './src/pages/home/index.js'
    },
    output: {
        path: '/Users/jian/Documents/sina/webpack-barbarian-test/dist',
        filename: '[name].js',
        publicPath: '/'
    },
    resolve: {
        extensions: ['.js', 'jsx', '.vue', '.json'],
        alias: {}
    },
    module: {
        rules: [{
            test: /\.js$/,
            loader: 'babel-loader',
            include: ['/Users/jian/Documents/sina/src', '/Users/jian/Documents/sina/test'],
            options: {
                presets: [['env', {
                    modules: false,
                    targets: {
                        browsers: ['> 1%', 'last 2 versions', 'not ie <= 8']
                    }
                }], 'stage-2'],
                plugins: ['transform-runtime']
            }
        },
        {
            test: /\.pug$/,
            loader: 'pug-loader',
            options: {
                pretty: true
            },
            exclude: ['node_modules']
        },
        {
            test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
            loader: 'url-loader',
            options: {
                limit: 10000,
                name: 'static/img/[name].[hash:7].[ext]'
            }
        },
        {
            test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
            loader: 'url-loader',
            options: {
                limit: 10000,
                name: 'static/media/[name].[hash:7].[ext]'
            }
        },
        {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
            loader: 'url-loader',
            options: {
                limit: 10000,
                name: 'static/fonts/[name].[hash:7].[ext]'
            }
        },
        {
            test: /\.css$/,
            use: [{
                loader: 'css-loader',
                options: {
                    sourceMap: true
                }
            },
            {
                loader: 'postcss-loader',
                options: {
                    sourceMap: true
                }
            }]
        },
        {
            test: /\.less$/,
            use: [{
                loader: 'css-loader',
                options: {
                    sourceMap: true
                }
            },
            {
                loader: 'postcss-loader',
                options: {
                    sourceMap: true
                }
            },
            {
                loader: 'less-loader',
                options: {
                    sourceMap: true
                }
            }]
        },
        {
            test: /\.sass$/,
            use: [{
                loader: 'css-loader',
                options: {
                    sourceMap: true
                }
            },
            {
                loader: 'postcss-loader',
                options: {
                    sourceMap: true
                }
            },
            {
                loader: 'sass-loader',
                options: {
                    indentedSyntax: true,
                    sourceMap: true
                }
            }]
        },
        {
            test: /\.scss$/,
            use: [{
                loader: 'css-loader',
                options: {
                    sourceMap: true
                }
            },
            {
                loader: 'postcss-loader',
                options: {
                    sourceMap: true
                }
            },
            {
                loader: 'sass-loader',
                options: {
                    sourceMap: true
                }
            }]
        }]
    },
    devtool: 'cheap-module-eval-source-map',
    plugins: [HotModuleReplacementPlugin {
        options: {},
        multiStep: undefined,
        fullBuildTimeout: 200,
        requestTimeout: 10000
    },
    HtmlWebpackPlugin {
        options: {
            template: 'src/pages/home/index.html',
            templateParameters: [Function: templateParametersGenerator],
            filename: 'home.html',
            hash: true,
            inject: true,
            compile: true,
            favicon: false,
            minify: false,
            cache: true,
            showErrors: true,
            chunks: ['manifest', 'vendor', 'home'],
            excludeChunks: [],
            chunksSortMode: 'auto',
            meta: {},
            title: 'Webpack App',
            xhtml: false,
            injuct: true
        }
    }]
}
2
  • 14
    try installing css-loader, npm install --save-dev css-loader Commented Aug 17, 2018 at 8:09
  • 1
    already have [email protected] installed Commented Aug 17, 2018 at 8:11

1 Answer 1

42

Figured out how this err happened

I'm developing a npm module and using npm link to test it on my local computer.

It seems that css-loader and post-loader also need to be installed in the test directory.

so npm install css-loader -D worked.

thx @Aritra Chakraborty.

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

1 Comment

The same happened to me when using a file dependency, e.g. "mylib": "file:./mylib". Thanks for pointing me into the right direction

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.