help icon indicating copy to clipboard operation
help copied to clipboard

EACCES error when trying to run "node index.js" from CMD

Open philipjani opened this issue 8 years ago • 6 comments

  • 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. (C:\Users\Phil\Documents\node\new_project\index.js:9:8) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12)

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}');
});

philipjani avatar Mar 15 '18 02:03 philipjani

try a higher port please, say a 5 digit number.

gireeshpunathil avatar Mar 15 '18 06:03 gireeshpunathil

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}`);
});

gireeshpunathil avatar Mar 15 '18 06:03 gireeshpunathil

Thanks. That looks like it did the trick!

philipjani avatar Mar 15 '18 11:03 philipjani

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.

aman071 avatar May 03 '18 12:05 aman071

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.

gireeshpunathil avatar May 05 '18 06:05 gireeshpunathil

I tried this but it still returns
image. This is my first time of doing Node.js. Thanks.

trustidkid avatar Apr 09 '19 11:04 trustidkid