Webpack can't find module css-loader (Error: Cannot find module '.../webpack_tutorial/node_modules/css-loader/dist/cjs.js')

Viewed 406

I'm currently learning webpack with the following tutorial on YouTube: https://www.youtube.com/watch?v=MpGLUVbqoYQ&t=1337s&ab_channel=freeCodeCamp.org, i'm on minute 43.

I want that Webpack loads my css, but when I run "node start", and the error "Cannot find module css-loader" occurs, although I have it installed properly.

This is my webpack.config.js:

const path = require('path');
module.exports = {
    mode: "development",
    entry: "./src/index.js",
    output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist")
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ["css-loader"]
            },
        ],
    }
}

package.json:

{
  "name": "webpack_tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack --config webpack.config.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "webpack": "^5.9.0",
    "webpack-cli": "^4.2.0"
  },
  "devDependencies": {
    "css-loader": "^5.0.1",
    "style-loader": "^2.0.0"
  }
}

index.js:

import { run } from "./app/app";
import "./main.css";
import { AlertService } from "./app/alert.service";
import { ComponentService } from "./app/component.service";
const alertService = new AlertService();
const componentService = new ComponentService();
run(alertService, componentService);

Folder structure:

  • dist
  • node_modules
  • src
    • app
      • some other files...
    • index.js
    • main.css
  • package.json
  • webpack.config.js

Has anyone an idea why webpack is not finding this package?

0 Answers
Related