我在最近安装webpack-dev-server
后总是启动失败,尝试了各种办法,都不管用。
本地环境
- macOS 10.12.6
- node v8.11.4
- npm v5.6.0
- 我是用
cnpm
安装的
错误提示
vents.js:183
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND localhost
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
依赖
{
"name": "webpack-guides",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server",
"watch": "webpack --watch",
"server": "node server.js",
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^0.1.19",
"express": "^4.16.3",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2",
"webpack-dev-middleware": "^3.4.0",
"webpack-dev-server": "^3.1.9"
},
"dependencies": {
"lodash": "^4.17.11"
}
}
配置
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
entry: {
app: './src/index.js',
print: './src/print.js'
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
port: 9000
},
plugins: [
// new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Output Mangement'
})
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
};
我尝试了
- 删除
/node-modules
重新安装 - 安装依赖
ws
(收到的资料) - 关闭本地
8080
与80
接口上的服务(搜到资料大部分说端口冲突) - 将端口设置为其他的
9000
- 安装其他版本的
webpack-dev-server
- 我使用
webpack-dev-middleware
与express
配合的服务器可以使用
以上都不能解决
问题解决
最终还是
/node_modules
依赖包的问题,归根究底,是网络的问题。我之前是使用手机分享的网络(中国移动)安装依赖的。今天使用宽带网络(长城宽带)安装依赖,一次成功。
排除
cnpm
的问题,宽带使用npm
与cnpm
都试过,一次成功。总结一下
说的直白点debug就是一个不断排除与不断定位寻找原因的过程
遇到这种神奇的问题一般经过几步
定位BUG
寻找思路
选择并解决问题