javascript 找不到模块:错误:无法解析“css-loader”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51890932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:44:26  来源:igfitidea点击:

Module not found: Error: Can't resolve 'css-loader'

javascriptcssnode.jswebpackcss-loader

提问by acui145

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

我正在使用 css-loader 并收到以下错误:

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 中的
错误未找到模块:错误:无法解析 '/Users/jian/Documents/sina/webpack-barbarian-test'
@ ./src/ 中的'css-loader' 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:

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

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

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

webpack.config.js:

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
        }
    }]
}

回答by acui145

Figured out how this err happened

弄清楚这个错误是如何发生的

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

我正在开发一个 npm 模块并npm link用于在我的本地计算机上对其进行测试。

It seems that css-loaderand post-loaderalso need to be installed in the test directory.

看来,css-loaderpost-loader还需要被安装在测试目录。

so npm install css-loader -Dworked.

所以npm install css-loader -D工作。

thx @Aritra Chakraborty.

谢谢@Aritra Chakraborty。