Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpackDevServer continuously console: WebSocket connection to 'ws://localhost:3003/ws' failed

I'd config and start my local webpackDevServer-based react project, it shows well on browser page ,but when I opened my dev-tools ,it just continuously console out the below errors, apparently,webpackDevServer is disconnect and reconnect again and again.

WebSocketClient.js:13 WebSocket connection to 'ws://localhost:3003/ws' failed: [webpack-dev-server] Event {isTrusted: true, type: 'error', target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …} console error info image

And my local config info is as below.

webpack.config.dev.js

const path = require('path');
const paths = require('./paths.js');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: {
    index: './src/index.js',
  },
  devServer:{
    port: 3003
  },
  output: {
    filename: '[name].bundle.js',
  },
  devtool: 'inline-source-map',
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Development',
      template: paths.appHtml
    }),
  ]
};

start.js

"use strict";

const Webpack = require("webpack");
const WebpackDevServer = require('webpack-dev-server');
const webpackConfig = require("../config/webpack.config.dev");

const compiler = Webpack(webpackConfig);
const devServerOptions = { ...webpackConfig.devServer, open: true };
const server = new WebpackDevServer(devServerOptions, compiler);

server.startCallback(() => {
  console.log('Successfully started server on http://localhost:3003');
});

package.json->scripts

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "node ./scripts/build.js",
    "start": "node ./scripts/start.js"
  }

I'd config the devServer port as 3003 aleady.

could any guys please give me some instructions how to solve this issue? Really looking forward to your answer!

like image 208
Anne Avatar asked Mar 14 '26 23:03

Anne


1 Answers

Try to configure the devServer.host properly. This worked for my case.

  devServer: {
    port: 3003,
    host: "127.0.0.1"  // <-- this
  },
like image 139
kaorukobo Avatar answered Mar 19 '26 12:03

kaorukobo