EACCES error when trying to run "node index.js" from CMD
- Node.js Version: 8.10.0
- OS: Windows 10
- Scope (install, code, runtime, meta, other?): When trying to run server on CMD with "node index.js"
- Module (and version) (if relevant):
C:\Users\Phil\Documents\node\new_project>node index.js events.js:183 throw er; // Unhandled 'error' event ^
Error: listen EACCES localhost
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1350:19)
at listenInCluster (net.js:1408:12)
at Server.listen (net.js:1503:5)
at Object.
index.js source code if it helps.
const http = require('http');
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
res.end('Welcome to Node!');
});
server.listen(hostname, port, () => {
console.log('server listening on host: ${hostname} port: ${port}');
});
try a higher port please, say a 5 digit number.
sorry, I did not look at your code. the listen takes port number as its first param: https://nodejs.org/dist/latest-v9.x/docs/api/http.html#http_server_listen
So please try this code:
const http = require('http');
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
res.end('Welcome to Node!');
});
server.listen(port, () => {
console.log(`server listening on host: ${hostname} port: ${port}`);
});
Thanks. That looks like it did the trick!
Hello, firstly thanks to @gireeshpunathil . this solved by issue too. I'm new to Node.js. I don't understand what the problem was here? Could you please explain? Just removing the hostname did the trick, why is that?
Thank You.
signature of listen API as per the doc:
server.listen([port][, host][, backlog][, callback]) for TCP servers
port: optional, default is an arbitrary port from the OS
host: optional, default is 0.0.0.0 (unspecified IPv4 address)
The problematic code had port and host order reversed:
server.listen(hostname, port, () => {
console.log('server listening on host: ${hostname} port: ${port}');
});
the problem is fixed by placing them in the right order.
I tried this but it still returns
. This is my first time of doing Node.js. Thanks.