I have this code in an index.js that I mostly left unmodified from the official Node.js documentation.

const https = require('https');
const fs = require('fs');
const cluster = require('node:cluster');
const numCPUs = require('node:os').cpus().length;
const process = require('node:process');

const livegame = require('./server-livegame');
const matchmaking = require('./server-matchmaking');

//Start ExpressJS
var express = require('express');
const { match } = require('assert');

var app = express();

if (cluster.isPrimary) {
  console.log(`Primary ${process.pid} is running`);

  for (let i = 0; i< numCPUs; i++){

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
  new livegame;
  new matchmaking;

Here is a simplified code for livegame/matchmaking that produces the error.

const https = require('https');
const fs = require('fs');
const mongoose = require('mongoose');

//Import Models
const LiveMatch = require('./models/livematch');

//Start ExpressJS
var express = require('express');
const { match } = require('assert');

//Interface Security
var options = {
  key: fs.readFileSync('key.pem', 'utf8'),
  cert: fs.readFileSync('cert.pem', 'utf8')

var httpsServer = https.createServer(options, app);

var app = express();

const chat = 
        puuid: String,
        name: String,
        roleID: String,
        message: String,

app.post(':id/chat', (req,res) => 
    //something here

I have livegame and matchmaking as separate .js files alongside the index.js whom I call to launch them programmatically as multiple instances. However, this is the error I get:

      throw er; // Unhandled 'error' event

Error: listen EADDRINUSE: address already in use :::443
    at Server.setupListenHandle [as _listen2] (node:net:1380:16)
    at listenInCluster (node:net:1428:12)
    at Server.listen (node:net:1516:7)
    at C:\Users\----\Documents\MG\src\server-matchmaking.js:25:66
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1407:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '::',
  port: 443

Based on my progress so far, I think the issue is with me creating more "dining space" instead of hiring more "workers".

How do I properly create instances of a server with proper load balancing using clusters?

🟢 Solution

The core problem here is that you're essentially trying to create multiple processes on the same port