0

I use node-cron to run the program at regular basis. I run the following code, but I got the error below that says the port is already in use.

I run node.js programs from command prompt in windows 10. Once I got this error, I closed the command prompt window in aim to end all the connection in use. And opened the window again and run the node program again. But I got the same error.

I should have used the port number 3128 only by this program. This program has been with no problem so far as I have run it multiple times. But suddenly this error started to occur today.

I want to know why the port is still in use even after the program ends, and want to know if node-cron program should be closed explicitly with some codes something like node-cron.closePort()?

sched_exec.js

  "use strict";
    
    const cron = require("node-cron");
    const express = require("express");
    const fs = require("fs");
    const sync_module = require('./sync');
    const logwriter = require('./logWriter/logwriter');
    
    const app = express();
    
    // schedule tasks to be run on the server
    //const exec_timing = "0 2 * * *";
    const exec_timing = "* * * * *";
    cron.schedule(exec_timing, function() {
      logwriter.info("start");
      
      //do something
    });
    
    app.listen(3128);

The error:

C:\inetpub\ftpfolder\syncSchedule>node sched_exec
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3128
    at Server.setupListenHandle [as _listen2] (net.js:1313:16)
    at listenInCluster (net.js:1361:12)
    at Server.listen (net.js:1447:7)
    at Function.listen (C:\inetpub\ftpfolder\syncSchedule\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\inetpub\ftpfolder\syncSchedule\sched_exec.js:21:5)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1340:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'EADDRINUSE',
  errno: 'EADDRINUSE',
  syscall: 'listen',
  address: '::',
  port: 3128
}

I did netstat -ab. It says, [node.exe] is the owner of the connection of the port 3128. however, I don't suppose any other node process is running.

I also have done research on task manage.

enter image description here

It has two node.js processes. But I don't know why. Should I ask this in another post?

Herbert
  • 540
  • 1
  • 6
  • 18

1 Answers1

0

You have another instance of this program, or some other program, running on your machine someplace.

EADDRINUSE is an operating system error announcing that your app.listen(3128) line could not listen for incoming connections on that port (3128) on your machine because some other program is already listening.

Your attempt to stop that program that's listening was unsuccessful.

How can you figure out what program is running? You can, of course, see all your running programs with Task Manager (ctrl-shift-escape).

You can also open a cmd shell (as administrator) and say netstat -ab The -b option shows you the executable file that's listening.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • thank you. but I know the port is in use. My question was bad, so I added some to clarify what I want to ask. – Herbert Sep 10 '20 at 13:23
  • thank you. I did netstat -ab. It says, [node.exe] is the owner of the connection of the port 3128. however, I don't suppose any other node process is running.. – Herbert Sep 10 '20 at 14:40