webpack 找不到模块,无法解析样式加载器的问题

xqk2d5yq  于 2022-11-13  发布在  Webpack
关注(0)|答案(1)|浏览(195)

我在./src/client/index. js中遇到这个错误错误模块未找到:错误:无法解析'style-loader'这是我的index.js,尽管我已正确指定路径

import { checkForName } from './js/nameChecker'
import { handleSubmit } from './js/formHandler'

import './styles/base.scss'
import './styles/footer.scss'
import './styles/form.scss'
import './styles/header.scss'
import './styles/resets.scss'

console.log(checkForName);

alert("I EXIST")
console.log("CHANGE!!");

这是我的配置文件,我尝试过npm安装样式加载程序css-loader --保存,但仍然不起作用

const path = require('path')
const webpack = require('webpack')
const HtmlWebPackPlugin = require("html-webpack-plugin")
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
    entry: './src/client/index.js',
    mode: 'development',
    devtool: 'source-map',
    module: {
        rules: [
            {
                test: '/\.js$/',
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.scss$/,
                use: [ 'style-loader', 'css-loader', 'sass-loader' ]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/client/views/index.html",
            filename: "./index.html",
        }),
        new CleanWebpackPlugin({
            // Simulate the removal of files
            dry: true,
            // Write Logs to Console
            verbose: true,
            // Automatically remove all unused webpack assets on rebuild
            cleanStaleWebpackAssets: true,
            protectWebpackAssets: false
        })
    ]
}

这是JSON代码

{
  "name": "example-project",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/server/index.js",
    "build-prod": "webpack --config webpack.prod.js",
    "build-dev": "webpack  --config webpack.dev.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.17.1",
    "webpack": "^4.35.3",
    "webpack-cli": "^3.3.5"
  },
  "devDependencies": {
    "@babel/core": "^7.10.2",
    "@babel/preset-env": "^7.10.2",
    "babel-loader": "^8.1.0",
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^3.6.0",
    "html-webpack-plugin": "^3.2.0",
    "node-sass": "^4.14.1",
    "sass-loader": "^7.3.1",
    "style-loader": "^0.23.1",
    "webpack-dev-server": "^3.7.2"
  }
}

提前感谢!

1mrurvl1

1mrurvl11#

我使用了你的配置,在更新了node-sass和sass-loader的版本后,我成功地构建了这个版本。你能尝试一下这些版本吗(在你的package.json中更新并运行npm install

"node-sass": "^7.0.3",
    "sass-loader": "10.3.1",

相关问题