Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: listen EACCES 0.0.0.0:443

I am trying to run a web project by NodeJS on my MacOS.

After npm install, npm start returns an error

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

Error: listen EACCES 0.0.0.0:443
    at Object._errnoException (util.js:1024:11)
    at _exceptionWithHostPort (util.js:1046:20)
    at Server.setupListenHandle [as _listen2] (net.js:1334:19)
    at listenInCluster (net.js:1392:12)
    at Server.listen (net.js:1476:7)
    at Object.<anonymous> (/Users/softtimur/Startup/PRODSERVER/tmp/WeCard/models/www:36:8)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

No other website is running on my Mac.

Does anyone know what's wrong? Do I need to configure something on my Mac?

Edit 1:

sudo npm start returns

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

Error: listen EADDRINUSE :::443
    at Object._errnoException (util.js:1024:11)
    at _exceptionWithHostPort (util.js:1046:20)
    at Server.setupListenHandle [as _listen2] (net.js:1351:14)
    at listenInCluster (net.js:1392:12)
    at Server.listen (net.js:1476:7)
    at Object.<anonymous> (/Users/softtimur/Startup/PRODSERVER/tmp/WeCard/models/www:36:8)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
like image 828
SoftTimur Avatar asked May 01 '18 06:05

SoftTimur


4 Answers

In order to listen on privileged ports you need root permissions to start the server; this applies to ports < 1024. You may use nginx as a reverse proxy server running on 443 and run your Node JS server alongside on non-privileged ports as an unprivileged user.

For more information on setting up Node JS application with nginx in production, follow the link: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-centos-7

like image 118
Abhishek Singh Avatar answered Oct 08 '22 08:10

Abhishek Singh


In UNIX-like systems, non-root users are unable to bind to ports lower than 1024.

This is a nuisance when proxying addresses on port 80. Typically, you end up sudoing all apps that must bind to such ports.

However, since kernel 2.6.24, you can use the setcap command to set specific capabilities to a program.

To enable all node programs to bind on any port lower than 1024, issue the following command:

sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/node

Note: If you don't know the location of node, follow below command.

sudo setcap 'cap_net_bind_service=+ep' `which node`
like image 29
Arjun Kava Avatar answered Oct 08 '22 08:10

Arjun Kava


I think you're probably looking to provide HTTPS to your application, if that's the case then usually with nodejs we don't serve using HTTPS directly from our node application. Instead we use nginx (a web server) to act as a "reverse proxy". This means nginx sits in front of our application, it listens on port 443 for us, and then sends requests through to our desired application.

Using nginx you can listen on port 443 and then redirect to multiple different services depending on the hostnames etc. For instance, I might have 3 web services running on one server, with nginx I can listen on port 443 for any HTTP connection. If the HTTP request is going to the host myblog.com it can send it off to a node service listening on port 8081. If the hostname on the request is myresume.com it might go off to a serivce on port 8082 and myprivatethings.com could go to port 8083.

Using something like nginx (many alternatives exist) you can manage multiple services with SSL/TLS (https) on one server.

like image 5
Elliot Blackburn Avatar answered Oct 08 '22 09:10

Elliot Blackburn


Foremost, do not run as root. That's asking for 'it'. Don't run your node web project as root.

Instead, use PM2 and authbind to do this:

// %user% is whatever user is running your code
sudo touch /etc/authbind/byport/443
sudo chown %user% /etc/authbind/byport/443
sudo chmod 755 /etc/authbind/byport/443

Next, add this alias to your ~/.bashrc or ~/.bash_profile:

alias pm2='authbind --deep pm2'

Then, try it with pm2:

pm2 start app.js
like image 4
4Z4T4R Avatar answered Oct 08 '22 09:10

4Z4T4R