最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't serve express app on firebase functions emulator | EADDRINUSE: address already in use :::3000 - Stack

programmeradmin2浏览0评论

I am testing express app deployment on firebase using firebase functions. But after using the mand firebase serve. I am getting EADDRINUSE: address already in use :::3000. Here is my index.js

const functions = require('firebase-functions');
const express = require('express');
const validator = require('email-validator');
const PORT = 3000;
const app = express();

/* JSON body parse*/
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/hello', (req, res, next) => {
  console.info('/hello call success ');
  res.send('Wele to Firebase Cloud Functions');
});

app.post('/emailValidate', async (req, res, next) => {
  const postData = req.body;
  if (postData.email) {
    console.info('/emailValidate call success ');
    res.json({ 'status': validator.validate(postData.email) });
  } else {
    console.warn('/emailValidate wrong input ');
    res.status(500).json({ 'status': 'wrong input' });
  }
});


app.listen(PORT, () => {
  console.info('Server is running on PORT:', PORT);
});

exports.app = functions.https.onRequest(app);    

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "body-parser": "^1.19.1",
    "email-validator": "^2.0.4",
    "express": "^4.17.2",
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

Error while opening URL functions[us-central1-app]: http function initialized (http://localhost:5001/{app-name}/us-central1/app).

 node:events:368
>        throw er; // Unhandled 'error' event
>        ^
>
>  Error: listen EADDRINUSE: address already in use :::3000
>      at Server.setupListenHandle [as _listen2] (node:net:1334:16)
>      at listenInCluster (node:net:1382:12)
>      at Server.listen (node:net:1469:7)
>      at Function.listen (\home\development\express-firebase\functions\node_modules\express\lib\application.js:618:24)
>      at Object.<anonymous> (D:\002_Research_Development\Web\development\express-firebase\functions\index.js:29:5)
>      at Module._pile (node:internal/modules/cjs/loader:1101:14)
>      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
>      at Module.load (node:internal/modules/cjs/loader:981:32)
>      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
>      at Module.require (node:internal/modules/cjs/loader:1005:19)
>  Emitted 'error' event on Server instance at:
>      at emitErrorNT (node:net:1361:8)
>      at processTicksAndRejections (node:internal/process/task_queues:83:21) {
>    code: 'EADDRINUSE',
>    errno: -4091,
>    syscall: 'listen',
>    address: '::',
>    port: 3000
>  }

Also, I have checked already there was nothing running at the defined PORT. Even tried changing the PORT number in index.js but the issue persist.

Any help would be much appreciated. :)

I am testing express app deployment on firebase using firebase functions. But after using the mand firebase serve. I am getting EADDRINUSE: address already in use :::3000. Here is my index.js

const functions = require('firebase-functions');
const express = require('express');
const validator = require('email-validator');
const PORT = 3000;
const app = express();

/* JSON body parse*/
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/hello', (req, res, next) => {
  console.info('/hello call success ');
  res.send('Wele to Firebase Cloud Functions');
});

app.post('/emailValidate', async (req, res, next) => {
  const postData = req.body;
  if (postData.email) {
    console.info('/emailValidate call success ');
    res.json({ 'status': validator.validate(postData.email) });
  } else {
    console.warn('/emailValidate wrong input ');
    res.status(500).json({ 'status': 'wrong input' });
  }
});


app.listen(PORT, () => {
  console.info('Server is running on PORT:', PORT);
});

exports.app = functions.https.onRequest(app);    

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "body-parser": "^1.19.1",
    "email-validator": "^2.0.4",
    "express": "^4.17.2",
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

Error while opening URL functions[us-central1-app]: http function initialized (http://localhost:5001/{app-name}/us-central1/app).

 node:events:368
>        throw er; // Unhandled 'error' event
>        ^
>
>  Error: listen EADDRINUSE: address already in use :::3000
>      at Server.setupListenHandle [as _listen2] (node:net:1334:16)
>      at listenInCluster (node:net:1382:12)
>      at Server.listen (node:net:1469:7)
>      at Function.listen (\home\development\express-firebase\functions\node_modules\express\lib\application.js:618:24)
>      at Object.<anonymous> (D:\002_Research_Development\Web\development\express-firebase\functions\index.js:29:5)
>      at Module._pile (node:internal/modules/cjs/loader:1101:14)
>      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
>      at Module.load (node:internal/modules/cjs/loader:981:32)
>      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
>      at Module.require (node:internal/modules/cjs/loader:1005:19)
>  Emitted 'error' event on Server instance at:
>      at emitErrorNT (node:net:1361:8)
>      at processTicksAndRejections (node:internal/process/task_queues:83:21) {
>    code: 'EADDRINUSE',
>    errno: -4091,
>    syscall: 'listen',
>    address: '::',
>    port: 3000
>  }

Also, I have checked already there was nothing running at the defined PORT. Even tried changing the PORT number in index.js but the issue persist.

Any help would be much appreciated. :)

Share Improve this question asked Jan 8, 2022 at 10:40 Gishant SinghGishant Singh 331 silver badge4 bronze badges 1
  • 3 You're trying to run two servers on the same port. Just the express port to 4000 or something. – user5734311 Commented Jan 8, 2022 at 10:42
Add a ment  | 

7 Answers 7

Reset to default 3

Another application is using port 3000, you need to stop the application and/or kill the port which varies depending on your operating system.

Just to elaborate on the previous answers, I was using an environment variable named “PORT” in the app.listen function which was causing the error, when I hardcoded the port number in the listen function it was solved

The error occurs because you configured your Express app to listen on port 3000. If you serve your function through Firebase, such as by using firebase serve or firebase emulators:start, you do not need to configure your Express to listen on any port.

I also had the same problem and solved it by deleting app.listen at index.js.

In my case, I was using the key "PORT" in my .env file and reading it like this:

// Start the server
const port = process.env.PORT || 3000;

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

It turns out, PORT is a reserved name in Firebase functions. To fix this, I changed the name of my environment variable to OUT_PORT.

You need to stop the application on your local and/or kill the port which varies depending on your operating system.

After that, just deploy using the firebase CLI:

firebase deploy --project PROJECT_ID

Adding to the other answers here, you can simply add this check to programmatically not run app.listen while deploying

if (process.env.FIREBASE_CONFIG == undefined) {
    const port = process.env.PORT || 3000;
    app.listen(port, () => {
        console.log(`Server is running on port ${port}`);
    });
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论