javascript - throw er;Unhandled 'error' event - NodeJS - Stack Overflow

DescriptionI'm trying to run my Node application. npm run start I kept getting > [email protect

Description

I'm trying to run my Node application.

npm run start 

I kept getting

> [email protected] start /Users/bheng/Sites/BASE/api
> nodemon ./bin/index.js -w server

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/bheng/Sites/BASE/api/server/**/*
[nodemon] starting `node ./bin/index.js`
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8000
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at Server._listen2 (net.js:1258:14)
    at listen (net.js:1294:10)
    at Server.listen (net.js:1390:5)
    at EventEmitter.listen (/Users/bheng/Sites/BASE/api/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/Users/bheng/Sites/BASE/api/bin/index.js:8:5)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3
[nodemon] app crashed - waiting for file changes before starting...

Attempt

package.json

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "bin/index.js",
  "scripts": {
    "start": "./node_modules/.bin/nodemon ./bin/index.js -w server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bluebird": "^3.5.0",
    "body-parser": "^1.15.2",
    "cors": "^2.8.4",
    "express": "^4.14.0",
    "helmet": "^3.8.1",
    "jwt-decode": "^2.2.0",
    "jwt-express": "^1.1.0",
    "lodash": "^4.17.4",
    "morgan": "^1.7.0",
    "pg": "^6.2.4",
    "pg-hstore": "^2.3.2",
    "request": "^2.81.0",
    "request-promise": "^4.2.1",
    "sequelize": "^4.1.0"
  },
  "devDependencies": {
    "eslint": "^2.13.1",
    "eslint-config-airbnb": "^9.0.1",
    "eslint-plugin-import": "^1.10.2",
    "eslint-plugin-jsx-a11y": "^1.5.5",
    "eslint-plugin-react": "^5.2.2",
    "google-translate-api": "^2.3.0",
    "nodemon": "^1.11.0"
  }
}

./node_modules/.bin/nodemon

#!/usr/bin/env node
'use strict';
var cli = require('../lib/cli');
var nodemon = require('../lib/');
var options = cli.parse(process.argv);

nodemon(options);

var fs = require('fs');

// checks for available update and returns an instance
var defaults = require('lodash.defaults');
var pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json'));

require('update-notifier')({
  pkg: defaults(pkg, { version: '0.0.0' }),
}).notify();

./bin/index.js

// This will be our application entry. We'll setup our server here.
const http = require('http');
const app = require('../server/app.js'); // The express app we just created
const port = parseInt(process.env.PORT, 10) || 8000;

app.set('port', port);
// const server = http.createServer(app);
app.listen(port, () => {
    console.log(`The server is running at localhost:${port}`);
});

server/app.js

const helmet = require('helmet');
const cors = require('cors');
const express = require('express');
const jwt = require('jwt-express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const Bluebird = require('bluebird');
const lodash = require('lodash');

Promise = Bluebird;
_ = lodash;

const app = express();
app.use(helmet());
app.use(cors());
app.options('*', cors());

app.use(logger('dev'));
app.use(jwt.init('**********', {
    cookies: false
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

require('./routes')(app);

app.get('*', (req, res) => res.status(200).send({
  message: 'Wele to the beginning of nothingness.',
}));

app.use(function(err, req, res, next) {
    if (err.name == 'JWTExpressError') {
        res.status(401).send('401', {
            error: {
                message: err.message
            }
        });
    } else {
        next(err);
    }
});

module.exports = app;

How would one go about debugging this?

Description

I'm trying to run my Node application.

npm run start 

I kept getting

> [email protected] start /Users/bheng/Sites/BASE/api
> nodemon ./bin/index.js -w server

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/bheng/Sites/BASE/api/server/**/*
[nodemon] starting `node ./bin/index.js`
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8000
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at Server._listen2 (net.js:1258:14)
    at listen (net.js:1294:10)
    at Server.listen (net.js:1390:5)
    at EventEmitter.listen (/Users/bheng/Sites/BASE/api/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/Users/bheng/Sites/BASE/api/bin/index.js:8:5)
    at Module._pile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3
[nodemon] app crashed - waiting for file changes before starting...

Attempt

package.json

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "bin/index.js",
  "scripts": {
    "start": "./node_modules/.bin/nodemon ./bin/index.js -w server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bluebird": "^3.5.0",
    "body-parser": "^1.15.2",
    "cors": "^2.8.4",
    "express": "^4.14.0",
    "helmet": "^3.8.1",
    "jwt-decode": "^2.2.0",
    "jwt-express": "^1.1.0",
    "lodash": "^4.17.4",
    "morgan": "^1.7.0",
    "pg": "^6.2.4",
    "pg-hstore": "^2.3.2",
    "request": "^2.81.0",
    "request-promise": "^4.2.1",
    "sequelize": "^4.1.0"
  },
  "devDependencies": {
    "eslint": "^2.13.1",
    "eslint-config-airbnb": "^9.0.1",
    "eslint-plugin-import": "^1.10.2",
    "eslint-plugin-jsx-a11y": "^1.5.5",
    "eslint-plugin-react": "^5.2.2",
    "google-translate-api": "^2.3.0",
    "nodemon": "^1.11.0"
  }
}

./node_modules/.bin/nodemon

#!/usr/bin/env node
'use strict';
var cli = require('../lib/cli');
var nodemon = require('../lib/');
var options = cli.parse(process.argv);

nodemon(options);

var fs = require('fs');

// checks for available update and returns an instance
var defaults = require('lodash.defaults');
var pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json'));

require('update-notifier')({
  pkg: defaults(pkg, { version: '0.0.0' }),
}).notify();

./bin/index.js

// This will be our application entry. We'll setup our server here.
const http = require('http');
const app = require('../server/app.js'); // The express app we just created
const port = parseInt(process.env.PORT, 10) || 8000;

app.set('port', port);
// const server = http.createServer(app);
app.listen(port, () => {
    console.log(`The server is running at localhost:${port}`);
});

server/app.js

const helmet = require('helmet');
const cors = require('cors');
const express = require('express');
const jwt = require('jwt-express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const Bluebird = require('bluebird');
const lodash = require('lodash');

Promise = Bluebird;
_ = lodash;

const app = express();
app.use(helmet());
app.use(cors());
app.options('*', cors());

app.use(logger('dev'));
app.use(jwt.init('**********', {
    cookies: false
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

require('./routes')(app);

app.get('*', (req, res) => res.status(200).send({
  message: 'Wele to the beginning of nothingness.',
}));

app.use(function(err, req, res, next) {
    if (err.name == 'JWTExpressError') {
        res.status(401).send('401', {
            error: {
                message: err.message
            }
        });
    } else {
        next(err);
    }
});

module.exports = app;

How would one go about debugging this?

Share Improve this question edited Dec 7, 2019 at 1:00 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Sep 3, 2017 at 0:21 code-8code-8 58.9k120 gold badges391 silver badges670 bronze badges 3
  • 1 Error: listen EADDRINUSE :::8000 You have something already running on port 8000... – Keith Commented Sep 3, 2017 at 0:30
  • switch the port to a different port. – codejockie Commented Sep 3, 2017 at 1:01
  • I can change to diff port. I don't mind, but how to find out exactly what app or process using that port ? Do you guys know ? – code-8 Commented Sep 3, 2017 at 14:24
Add a ment  | 

3 Answers 3

Reset to default 2

Just as Abhijeet and Keith pointed out, there is another process running on port 8000.

To kill all NodeJS processes and subsequently freeing the 8000 port so you can use on your app, use this code:

killall node

Or as djburdick pointed right here:

ps aux | grep node to get a list of the NodeJS processes and their IDs.

and

kill -9 PID - changing PID with the process ID that is using the 8000 port.

Error: listen EADDRINUSE :::8000

Your error message just told you the reason, 8000 is been used by some other app as well, try running it on diff port.

To get the lists of port you can use safely, please refer https://www.browserstack./question/664

Error: listen EADDRINUSE :::8000

  • Find out which app is consuming port 8000: lsof -n | grep LISTEN | grep :8000
  • The first column will indicate the process
  • Either kill the process or figure out what app needs to be gracefully shutdown.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744810899a4595053.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信