webpack-dev-server provides a Node.js API which can be used directly in Node.js runtime.
To start using thewebpack-dev-server Node.js API, first installwebpack andwebpack-dev-server if you haven’t yet:
npminstall --save-dev webpack webpack-dev-serverThen require the modules in your Node.js script:
const Webpack=require('webpack');const WebpackDevServer=require('webpack-dev-server');It instructswebpack-dev-server instance to start the server.
server.js
const Webpack=require('webpack');const WebpackDevServer=require('webpack-dev-server');const webpackConfig=require('./webpack.config.js');const compiler=Webpack(webpackConfig);const devServerOptions={...webpackConfig.devServer, open:true};const server=newWebpackDevServer(devServerOptions, compiler);construnServer=async()=>{ console.log('Starting server...');await server.start();};runServer();And then run the server with the following command:
node server.jsIt instructswebpack-dev-server instance to start the server and then run the callback function.
server.js
const Webpack=require('webpack');const WebpackDevServer=require('webpack-dev-server');const webpackConfig=require('./webpack.config.js');const compiler=Webpack(webpackConfig);const devServerOptions={...webpackConfig.devServer, open:true};const server=newWebpackDevServer(devServerOptions, compiler);server.startCallback(()=>{ console.log('Successfully started server on http://localhost:8080');});And then run the server with the following command:
node server.jsIt instructswebpack-dev-server instance to stop the server.
server.js
const Webpack=require('webpack');const WebpackDevServer=require('webpack-dev-server');const webpackConfig=require('./webpack.config.js');const compiler=Webpack(webpackConfig);const devServerOptions={...webpackConfig.devServer, open:true};const server=newWebpackDevServer(devServerOptions, compiler);construnServer=async()=>{ console.log('Starting server...');await server.start();};conststopServer=async()=>{ console.log('Stopping server...');await server.stop();};runServer();setTimeout(stopServer,5000);And then run the server with the following command:
node server.jsIt instructswebpack-dev-server instance to stop the server and then run the callback function.
server.js
const Webpack=require('webpack');const WebpackDevServer=require('webpack-dev-server');const webpackConfig=require('./webpack.config.js');const compiler=Webpack(webpackConfig);const devServerOptions={...webpackConfig.devServer, open:true};const server=newWebpackDevServer(devServerOptions, compiler);server.startCallback(()=>{ console.log('Successfully started server on http://localhost:8080');});conststopServer=()=> server.stopCallback(()=>{ console.log('Server stopped.');});setTimeout(stopServer,5000);And then run the server with the following command:
node server.jsReturns the internalIPv4/IPv6 address asynchronously.
server.js
const WebpackDevServer=require('webpack-dev-server');constlogInternalIPs=async()=>{const localIPv4=await WebpackDevServer.internalIP('v4');const localIPv6=await WebpackDevServer.internalIP('v6'); console.log('Local IPv4 address:', localIPv4); console.log('Local IPv6 address:', localIPv6);};logInternalIPs();Returns the internalIPv4/IPv6 address synchronously.
server.js
const WebpackDevServer=require('webpack-dev-server');const localIPv4= WebpackDevServer.internalIPSync('v4');const localIPv6= WebpackDevServer.internalIPSync('v6');console.log('Local IPv4 address:', localIPv4);console.log('Local IPv6 address:', localIPv6);