While it's recommended to run webpack-dev-server
via the CLI, you may also
choose to start a server via the API.
This example demonstrates using start
method. It instructs webpack-dev-server
instance to start the server.
const Webpack = require("webpack");
const WebpackDevServer = require("webpack-dev-server");
const webpackConfig = require("./webpack.config");
const compiler = Webpack(webpackConfig);
const devServerOptions = { ...webpackConfig.devServer };
const server = new WebpackDevServer(devServerOptions, compiler);
const runServer = async () => {
console.log("Starting server...");
await server.start();
};
runServer();
Use the following command to run this example:
node server.js
- Open
http://localhost:8080/
in your preferred browser. - You should see the text on the page itself change to read
Success!
.