Implementation of webpack-dev-server to build a local server

Implementation of webpack-dev-server to build a local server

Preface

When we use webpack to package, we find that every time we update a little code, we need to repackage it, which is very troublesome. We hope to build a server locally, and then write new code to be automatically detected. At this time, we need to use webpack-dev-server

webpack-deb-server

Webpack provides an optional local development server. This local server is built on node.js and uses the express framework internally, which can achieve the browser automatic refresh to display the modified results we want.

Since it is a separate module, we need to install it before using it. The command is as follows:

npm install -D webpack-dev-server 

After the installation is complete, we also need to configure it in webpack. The object of configuration is devServer, which also has many properties. The commonly used properties are as follows:

  • contentBase: Which file provides local service? The default is the root file. Here we need to fill in ./dist
  • port: port number, the default is 8080
  • Inline: The page is refreshed in real time
  • historyApiFallBack: In SPA (single page application) pages, rely on HTML5 history mode

The webpack.config.js configuration is as follows:

module.exports = {
   devServer: {
        contentBase: "./dist",
        inline: true,
    },
}

Next, let's add a script command to the package.json file:

"scripts": {
    "dev": "webpack serve"
  },

dev represents the development environment, and the above configuration is complete

webpack-dev-server startup error

Then we start the command npm run dev, and the program reports the following error:

Error: Cannot find module 'webpack-cli/bin/config-yargs'

The reason is the version problem of webpack-cli. Let's first look at our version below

"webpack": "^5.44.0",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"

Solution 1

Lower the version of webpack-cli from 4 to 3

1. Uninstall webpack-cli

npm uninstall webpack-cli

2. Install webpack-cli@3

npm install webpack-cli@3 -D

Then the startup will not report an error, but this is only a temporary solution. We recommend the second solution

Solution 2

Change the configuration in scripts and change the original webpack-dev-serve to webpack serve

"scripts": {
    "dev": "webpack serve --open --mode development"
},

Finally, we enter npm run dev in the terminal to start normally, and the webpage will be opened automatically because we added the parameter --open. If you want to open it manually, just remove --open.

Solve the port occupation problem

If you have already started a project with vue+webpack, but you execute npm run dev again, the following error will be reported

Error: listen EADDRINUSE: address already in use 127.0.0.1:8080

The reason is that the default port we started last time was 8080. This time you start a project again and the port is still 8080, but port 8080 is already occupied. The solution is that we only need to kill the PID process number corresponding to port 8080.

First find the process ID corresponding to port 8080

lsof -i:8080

After finding the corresponding PID, use the kill command to kill it.

kill -9 PID process number

This is the end of this article about building a local server with webpack-dev-server. For more information about building a local server with webpack-dev-server, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of how to use webpack-dev-server
  • Detailed explanation of Webpack-dev-server proxy usage
  • Tutorial on configuring webpack-dev-server using webpack3.0
  • A brief discussion on the configuration and use of webpack-dev-server
  • How to use webpack-dev-server to handle cross-domain requests
  • Detailed explanation of the simple use of webpack-dev-server
  • Webpack-dev-server remote access configuration method
  • Webpack-dev-server automatic update page method

<<:  Zabbix configures DingTalk's alarm function with pictures

>>:  Several ways to add timestamps in MySQL tables

Recommend

Tutorial on installing rabbitmq using yum on centos8

Enter the /etc/yum.repos.d/ folder Create rabbitm...

Canonical enables Linux desktop apps with Flutter (recommended)

Google's goal with Flutter has always been to...

Teach you to implement a simple promise step by step

Table of contents Step 1: Build the framework Ste...

How to use Navicat to export and import mysql database

MySql is a data source we use frequently. It is v...

NodeJS realizes image text segmentation

This article shares the specific code of NodeJS t...

MySQL startup error InnoDB: Unable to lock/ibdata1 error

An error message appears when MySQL is started in...

html+css+js to realize the function of photo preview and upload picture

Preface: When we are making web pages, we often n...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...

A brief introduction to React

Table of contents 1. CDN introduction 1.1 react (...

Detailed explanation of JavaScript closure issues

Closures are one of the traditional features of p...