Search code examples
node.jswindowsnetwork-programming

Error: listen EACCES: permission denied 0.0.0.0:3001


I have gone through many answers here, none of them fixed my error. Here What I have tried so far.

Environment:

win 10 pro v2004 build 19041.29

node v12.14.1
  1. used netstat -a -b to see if this port is being used by any other process. No process is using this port, also tried switching the port to 3000 getting same error.

  2. I use docker-desktop and wsl2, so also disabled all virtual network adapter one by one. Tried after killing all other docker services.

  3. Restarted my pc.

  4. Tried running npm start through powershell as administrator

Server.js

const app = require('./src/app');

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Litening on port ${port}...`);
});

Error:

events.js:200
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES: permission denied 0.0.0.0:3001
    at Server.setupListenHandle [as _listen2] (net.js:1289:21)
    at listenInCluster (net.js:1354:12)
    at Server.listen (net.js:1442:7)
    at Function.listen (C:\Users\sujeet\project\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\sujee\CustomerAPIs\server.js:4:5)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1333:8)
    at processTicksAndRejections (internal/process/task_queues.js:81:21) {
  code: 'EACCES',
  errno: 'EACCES',
  syscall: 'listen',
  address: '0.0.0.0',
  port: 3001
}

Solution

  • For Windows users encountering the 'EACCES: permission denied' error when attempting to bind a Node.js server to a specific port, follow these steps:

    1. Run PowerShell as an Administrator: Ensure elevated privileges for system-level commands.

    2. Stop the "winnat" service:

    net stop winnat
    

    This halts the Windows Network Address Translation (NAT) service, responsible for dynamically translating private IP addresses to public ones, essential for internet connectivity.

    1. Restart the "winnat" service:
    net start winnat
    

    This command restores the winnat service, reestablishing NAT functionality.

    Technical Explanation:

    • Port Binding and Permissions: The 'EACCES' error arises from inadequate permissions during the attempt to bind the Node.js server to a specific IP address and port. Running PowerShell as an Administrator grants the necessary permissions for this operation.

    • Windows NAT Service ("winnat"): 'winnat' is a critical service responsible for network address translation. During server port binding, conflicts may occur as 'winnat' dynamically manages the translation of private IP addresses to public ones, impacting the binding process.

    • Dynamic Service Interaction: Temporarily stopping 'winnat' dynamically modifies the state of the NAT service. This is a strategic measure to create a temporary window during which the Node.js server can bind to the port without contention from the NAT service.

    • Temporary Network Adjustment: The command net stop winnat instructs Windows to temporarily suspend the NAT service. This suspension is a tactical maneuver to provide a brief opportunity for the Node.js server to bind to the port without encountering permission issues.

    • Node.js Server Binding Attempt: With 'winnat' temporarily halted, the Node.js server can successfully bind to the desired port. This step is critical in ensuring that the server can initiate and establish connections without being denied due to insufficient permissions.

    • Network State Restoration: The subsequent command net start winnat restarts the 'winnat' service, restoring normal network functionality. This ensures that any temporary adjustments made for the server binding process are reverted, preventing potential disruptions to other network-dependent processes.

    This sequence of commands is intricately designed to navigate the complexities of Windows services, specifically addressing the challenges posed by the 'winnat' service during the process of binding a Node.js server to a port.