问题描述:
在使用npm run dev启动一个前端项目的时候,经常会遇到某个端口被占用,导致无法正常访问页面,报错如下:
Error: listen EADDRINUSE: address already in use 0.0.0.0:8090
at Server.setupListenHandle [as _listen2] (net.js:1331:16)
at listenInCluster (net.js:1379:12)
at doListen (net.js:1516:7)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '0.0.0.0',
port: 8090
}
解决办法:
1. 可以使用以下命令查找当前端口被哪些进程占用了
sudo lsof -i tcp:8090
2. 当执行以上命令后,找到了这些进程(是被nginx占用了)
xxx@xxx-xxx:/var/www/client/src$ lsof -i tcp:8090
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 807863 zph 21u IPv4 17021269 0t0 TCP *:8090 (LISTEN)
3.使用以下命令杀死这些进程
xxx@xxx-xxx:/var/www/client/src$ kill -9 807863
4. 重新运行npm run dev启动前端项目,恢复正常
xxx@xxx-xxx:/var/www/client/src npm run dev
> [email protected] dev /var/www/client> preact watch
BUILD_FOR undefined
Build [ ] 0% (0.0s) compiling
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:8090/
<i> [webpack-dev-server] On Your Network (IPv4): http://xxx:8090/
<i> [webpack-dev-server] Content not from webpack is served from '/var/www/client/src' directory
<i> [webpack-dev-server] 404s will fallback to '/index.html'
Compiled successfully!
You can view the application in browser.
Local: http://0.0.0.0:8090
On Your Network: http://xxx:8090
Build [ ] 0% (12.3s) compiling
Compiled successfully!
You can view the application in browser.
Local: http://0.0.0.0:8090
On Your Network: http://xxx:8090