Webpack Dev Server



Webpack-dev-server is Webpack's officially supported CLI-based tool for starting a static server for your assets. While you don't need any CLI tools to use Webpack, webpack-dev-server gives you a single command that starts a static server with built-in live reload.

Use webpack with a development server that provideslive reloading. This should be used for development only.

It uses webpack-dev-middleware under the hood, which providesfast in-memory access to the webpack assets.

Possible duplicate of Webpack Dev Server (webpack-dev-server) Hot Module Replacement (HMR) Not Working – Shubham Khatri Aug 30 '16 at 11:47 The linked question does not help, especially since it does not even have a confirmed answer. In your case, the script webpack-dev-server is already installed somewhere inside./nodemodules directory, but system does not know how to access it. So, to access the command webpack-dev-server, you need to install the script in global scope as well. $ npm install webpack-dev-server -g Here.

Table of Contents

  • Usage

Getting Started

First things first, install the module:

Note: While you can install and run webpack-dev-server globally, we recommendinstalling it locally. webpack-dev-server will always use a local installationover a global one.

Usage

There are two main, recommended methods of using the module:

With the CLI

The easiest way to use it is with the CLI. In the directory where yourwebpack.config.js is, run:

Note: Many CLI options are available with webpack-dev-server. Explore this link.

With NPM Scripts

NPM package.json scripts are a convenient and useful means to run locally installedbinaries without having to be concerned about their full paths. Simply define ascript as such:

And run the following in your terminal/console:

NPM will automagically reference the binary in node_modules for you, andexecute the file or command.

The Result

Either method will start a server instance and begin listening for connectionsfrom localhost on port 8080.

webpack-dev-server is configured by default to support live-reload of files asyou edit your assets while the server is running.

See the documentation for more use cases and options.

Browser Support

Webpack Dev Server Not Working

While webpack-dev-server transpiles the client (browser) scripts to an ES5state, the project only officially supports the last two versions of majorbrowsers. We simply don't have the resources to support every whackybrowser out there.

If you find a bug with an obscure / old browser, we would actively welcome aPull Request to resolve the bug.

Support

We do our best to keep Issues in the repository focused on bugs, features, andneeded modifications to the code for the module. Because of that, we ask userswith general support, 'how-to', or 'why isn't this working' questions to try oneof the other support channels that are available.

Your first-stop-shop for support for webpack-dev-server should by the excellentdocumentation for the module. If you see an opportunity for improvementof those docs, please head over to the webpack.js.org repo and open apull request.

From there, we encourage users to visit the webpack Gitter chat andtalk to the fine folks there. If your quest for answers comes up dry in chat,head over to StackOverflow and do a quick search or open a newquestion. Remember; It's always much easier to answer questions that include yourwebpack.config.js and relevant files!

If you're twitter-savvy you can tweet #webpack with your questionand someone should be able to reach out and lend a hand.

If you have discovered a 🐛, have a feature suggestion, or would like to seea modification, please feel free to create an issue on Github. Note: The issuetemplate isn't optional, so please be sure not to remove it, and please fill itout completely.

Contributing

We welcome your contributions! Please have a read of CONTRIBUTING.md for more information on how to get involved.

Attribution

Webpack Dev Server

This project is heavily inspired by peerigon/nof5.

License

readme.txt
Do the following to reproduce:
1. yarn
2. node echo-server.js
3. yarn start (in other shell)
4. open Chrome and goto localhost:3000
5. open Chrome dev tools and issue multiple of the following requests in the console: fetch('/post', { method: 'POST', body: 'test'})
6. observe that every second request is a few hundred ms longer than every other
Note that this does not happen in Microsoft Edge and this does not happen if not proxying through webpack-dev-server.
dev-server.js

Webpack-dev-server Npm

constwebpack=require('webpack');
constWebpackDevServer=require('webpack-dev-server');
constconfig=require('./webpack.config');
constserver=newWebpackDevServer(webpack(config),{
publicPath: config.output.publicPath,
contentBase: './',
proxy: {
'/post': {
target: 'http://localhost:3001',
logLevel: 'debug',
}
}
});
server.listen(3000,'localhost',(err)=>{
if(err){
returnconsole.log(err);
}
console.log('Listening at http://localhost:3000/');
});
echo-server.js

Webpack-dev-server: Command Not Found

varhttp=require('http');
http.createServer(function(request,response){
response.writeHead(200);
request.pipe(response);
}).listen(3001);
package.json
{
'name': 'test-webpack-dev-server',
'version': '1.0.0',
'description': '...',
'main': 'test.html',
'license': 'MIT',
'scripts': {
'start': 'node dev-server.js'
},
'dependencies': {},
'devDependencies': {
'webpack': '^4.29.6',
'webpack-dev-server': '^3.3.1'
}
}
result.png
test.html
test.js
webpack.config.js
constpath=require('path');
module.exports={
mode: 'development',
entry: './test.js',
output: {
path: path.resolve('output'),
filename: 'main.bundle.js'
},
resolve: {
extensions: ['.ts','.tsx','.js','.json','.css'],
modules: [path.resolve('node_modules')]
},
};
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment