ulixee / hero

The web browser built for scraping
MIT License
857 stars 43 forks source link

Error: listen EADDRINUSE: address already in use #290

Closed cyai closed 1 week ago

cyai commented 2 months ago

I am getting Error: listen EADDRINUSE: address already in use whenever I try to open a browser instance using hero.

I tried to check if the port that I am running my node project is occupied but turns out that the port is free and no other services is running on it.

System Version: macOS 14.5 (23F79)

The entire error:


      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::8083
    at Server.setupListenHandle [as _listen2] (node:net:1872:16)
    at listenInCluster (node:net:1920:12)
    at Server.listen (node:net:2008:7)
    at addressHost (/Users/vardh/work/freelancing/aptask/linkedin-search/cloud/main/lib/RoutableServer.ts:86:12)
    at new Promise (<anonymous>)
    at RoutableServer.listen (/Users/vardh/work/freelancing/aptask/linkedin-search/cloud/main/lib/RoutableServer.ts:83:33)
    at CloudNode.startPublicServer (/Users/vardh/work/freelancing/aptask/linkedin-search/cloud/main/lib/CloudNode.ts:297:55)
    at CloudNode.listen (/Users/vardh/work/freelancing/aptask/linkedin-search/cloud/main/lib/CloudNode.ts:150:18)
    at getCoreHost (/Users/vardh/work/freelancing/aptask/linkedin-search/playgrounds/hero/index.ts:39:17)
    at new Hero (/Users/vardh/work/freelancing/aptask/linkedin-search/playgrounds/hero/index.ts:24:46)
Emitted 'error' event on WebSocketServer instance at:
    at Server.emit (node:events:530:35)
    at emitErrorNT (node:net:1899:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 8083
}

Node.js v20.11.0```
blakebyrnes commented 2 months ago

What version of Hero are you using, and can you share more of your setup?

cyai commented 2 months ago

Version: "@ulixee/hero-playground": "^2.0.0-alpha.28"

So in my project I have set up a bullmq queue to manage tasks and when a task is loaded in the queue then the hero instance is created and then further activities are performed. The error I am getting is right at the place where the hero instance is created:

return new Hero({
    showChromeInteractions: true,
    showChrome: true,
    userProfile: theStoredProfile,
});
blakebyrnes commented 2 months ago

Hero Playground is only meant to be used for testing. It's an easy start up and shut down after a single scrape project. I don't know if you might have another one stuck around in the background? It could help to switch to full stack: https://ulixee.org/docs/hero/advanced-concepts/deployment#fullstack

pedrosarkis commented 1 month ago

Hi. I was getting the exactly same error. Using playground, now I've made a simple code,

`const HeroCore = require('@ulixee/hero-core'); const { TransportBridge } = require('@ulixee/net'); const Hero = require('@ulixee/hero'); const { ConnectionToHeroCore } = require('@ulixee/hero');

const bridge = new TransportBridge(); const connectionToCore = new ConnectionToHeroCore(bridge.transportToCore); const heroCore = new HeroCore(); heroCore.addConnection(bridge.transportToClient);

async function routes(fastify, options) { fastify.get('/nyt', async (request, reply) => { let hero; try { hero = new Hero({ connectionToCore, showBrowser: false }); await hero.goto('https://www.nytimes.com/international/'); await hero.waitForPaintingStable(); const body = await hero.document.body.innerHTML; return body; } catch (error) { console.error('Error:', error); reply.status(500).send('An error occurred while fetching the page'); } finally { if (hero) { await hero.close(); } } }); }

module.exports = routes;`

And I deployed to render.com. And now I'm getting this error. Error: Error: Failed to launch chrome 124.0.6367.78: Please re-install the browser engine:

To be honest, I didn't find the documentation very clear in this regard. I read it exactly here: https://ulixee.org/docs/hero/advanced-concepts/deployment#fullstack

blakebyrnes commented 1 month ago

@pedrosarkis Your code looks right, and I would guess this works fine on a local machine. I think you're likely experiencing an issue that has to do with how your code got packaged up for your hosting service. Hero needs to have a copy of chrome downloaded, which it does through a User's cache dir. You're right that the directions are very unhelpful for how to get Hero running on these kinds of hosting services - they all tend to have quirks for what directories are available and what post install scripts can be run.

I don't have any idea how to help you troubleshoot if this is indeed the hosting service. If it doesn't work for you locally, then something else is going on, and you are welcome to provide more details.

If not, my guess is that it will have something to do with one of these:

Hero runs a postinstall script to download Chrome and a Socket Connect native library.

Hero will store data outside the node_modules to:

A linux deployment also sometimes needs extra dependencies to be installed via sudo $(npx install-browser-deps)