npm start 时报错 events.js:186 throw er; // Unhandled 'error' event

MAC 电脑
在执行 pm start 时报错

ccl-mac:rn_yxq CC$ npm start

> [email protected] start /Users/CC/Desktop/RNTest/rn_yxq
> node node_modules/react-native/local-cli/cli.js start

┌──────────────────────────────────────────────────────────────────────────────┐
│                                                                              │
│  Running Metro Bundler on port 8081.                                         │
│                                                                              │
│  Keep Metro running while developing on any JS projects. Feel free to        │
│  close this tab and run your own Metro instance if you prefer.               │
│                                                                              │
│  https://github.com/facebook/react-native                                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

events.js:186
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::8081
    at Server.setupListenHandle [as _listen2] (net.js:1298:14)
    at listenInCluster (net.js:1346:12)
    at Server.listen (net.js:1434:7)
    at /Users/CC/Desktop/RNTest/rn_yxq/node_modules/metro/src/index.js:253:20
    at new Promise ()
    at Object. (/Users/CC/Desktop/RNTest/rn_yxq/node_modules/metro/src/index.js:252:14)
    at Generator.next ()
    at asyncGeneratorStep (/Users/CC/Desktop/RNTest/rn_yxq/node_modules/metro/src/index.js:46:24)
    at _next (/Users/CC/Desktop/RNTest/rn_yxq/node_modules/metro/src/index.js:66:9)
Emitted 'error' event on WebSocketServer instance at:
    at Server.WebSocketServer._onServerError (/Users/CC/Desktop/RNTest/rn_yxq/node_modules/ws/lib/WebSocketServer.js:82:50)
    at Server.emit (events.js:214:15)
    at emitErrorNT (net.js:1325:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: 'EADDRINUSE',
  syscall: 'listen',
  address: '::',
  port: 8081
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node node_modules/react-native/local-cli/cli.js start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/CC/.npm/_logs/2019-09-12T03_14_54_656Z-debug.log

原因是端口被占用了.
检查端口占用命令. 根据报错提示, 我这里检查的是8081端口, 各自情况可能不一样

lsof -i:8081

ccl-mac:rn_yxq CC$ lsof -i:8081
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    6298   CC   24u  IPv6 0xf02143eb06cfe9f9      0t0  TCP *:sunproxyadmin (LISTEN)

杀掉当前进程

kill 6298

在执行

npm start

你可能感兴趣的:(React,Native)