Search code examples
javascriptwebpackwebpack-dev-server

Webpack-dev-server throws errors about files missing in node_modules/webpack-dev-server


I am trying to simply run webpack serve -c myconfig.js, but I keep getting these errors:

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 6:16-37
Module not found: Error: Can't resolve 'strip-ansi' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 8:13-32
Module not found: Error: Can't resolve './socket' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 10:14-34
Module not found: Error: Can't resolve './overlay' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 12:15-37
Module not found: Error: Can't resolve './utils/log' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 16:18-48
Module not found: Error: Can't resolve './utils/sendMessage' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 18:16-44
Module not found: Error: Can't resolve './utils/reloadApp' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 20:22-56
Module not found: Error: Can't resolve './utils/createSocketUrl' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

If I try to add hot reload, it gets even worse (additional errors, all about smth missing from node_modules).

Here's the code:

package.json

{
  "name": "webpack-example",
  "version": "1.0.0",
  "description": "",
  "private": "true",
  "scripts": {
    "dev": "npx webpack serve -c webpack.dev.js",
    "build": "npx webpack --config webpack.prod.js"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://[email protected]/iron.alex/webpack-example.git"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-webpack-plugin": "^4.0.0-alpha.0",
    "css-loader": "^5.2.4",
    "ftp-deploy": "^2.4.1",
    "html-loader": "^2.1.2",
    "html-webpack-plugin": "^5.3.1",
    "mini-css-extract-plugin": "^1.5.0",
    "node-sass": "^5.0.0",
    "sass": "^1.32.11",
    "sass-loader": "^11.0.1",
    "style-loader": "^2.0.0",
    "ts-loader": "^9.1.0",
    "typescript": "^4.2.4",
    "webpack": "^5.35.1",
    "webpack-cli": "^4.6.0",
    "webpack-dev-server": "^3.11.2",
    "webpack-merge": "^5.7.3"
  }
}

webpack.dev.js

const path = require("path");
const common = require("./webpack.common.js");
const { merge } = require("webpack-merge");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = merge(common, {
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dev"),
  },
  module: {
    rules: [
      {
        test: /\.scss$/i,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",
    }),
  ],
  mode: "development",
  //   https://www.youtube.com/watch?v=yR25JoybTxo
  devServer: {
    contentBase: path.join(__dirname, "dev"),
    compress: true,
    port: 9000,
  },
});

webpack.common.js

const path = require("path");

module.exports = {
  entry: {
    index: "./src/scripts/index.ts",
    library: "./src/scripts/library.ts",
  },
  resolve: {
    extensions: [".ts"],
  },
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
      {
        test: /\.html$/i,
        loader: "html-loader",
      },
    ],
  },
};

I tried reinstalling node_modules, but with no luck. I added an issue on github, but not sure what else to do/how to fix it.

https://github.com/webpack/webpack-dev-server/issues/3221

Any ideas what I'm missing?

Webpack builds without serve


Solution

  • The problem was in

    resolve: {
      extensions: [".ts"],
    },
    

    it should've been ['.ts','.js',/* other extentions */].

    See here for more info.