简体   繁体   中英

Webpack shows an error regarding css-loader when I run npm run build

when I run webpack it shows me following error. How to fix it?

ERROR in ./styles/image_viewer.css
Module build failed (from ./node_modules/css-loader/dist/cjs.js):
CssSyntaxError

(2:7) D:\web-components\styles\image_viewer.css Unknown word

  1 |
> 2 |       import API from "!../node_modules/style-loader/dist/runtime/injectStylesIntoStyleTag.js";
    |       ^
  3 |       import domAPI from "!../node_modules/style-loader/dist/runtime/styleDomAPI.js";
  4 |       import insertFn from "!../node_modules/style-loader/dist/runtime/insertBySelector.js";
 @ ./src/imageViewer.js 1:0-36
 @ ./src/app.js 2:0-23

Package.json file

{
  "name": "web-components",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "build": "webpack",
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.15.5",
    "@babel/preset-env": "^7.15.6",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.2.2",
    "babel-preset-env": "^1.7.0",
    "css-loader": "^6.3.0",
    "jest": "^27.2.2",
    "style-loader": "^3.3.0",
    "webpack": "^5.54.0",
    "webpack-cli": "^4.8.0"
  },
  "dependencies": {}
}

Webpack.config.js

const path = require('path')

const config = {
    entry: './src/app.js',
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {use: 'babel-loader', test: /\.js$/},
            {use: ['css-loader', 'style-loader'], test: /\.css$/}
        ]
    },
    mode: 'development'
};

module.exports = config;

First I used webpack 2.2.0-rc.0 version and since there were some issues I upgraded it to the latest version and that also didn't worked for me. Even I tried to downgrade versions of style-loader and css-loader, it didn't worked. Thanks

Solution

  1. Uninstall sass first

    npm uninstall node-sass

  2. Install sass again

    npm install node-sass --save-dev

    npm install sass-loader --save-dev

  3. Success

    npm run start/serve

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM