Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
philipjani opened this issue Mar 15, 2018 · 6 comments
Closed

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

philipjani opened this issue Mar 15, 2018 · 6 comments

Comments

@philipjani
Copy link

philipjani commented Mar 15, 2018

  • 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}');
});
@gireeshpunathil
Copy link
Member

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

@gireeshpunathil
Copy link
Member

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

@philipjani
Copy link
Author

Thanks. That looks like it did the trick!

@aman071
Copy link

aman071 commented May 3, 2018

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.

@gireeshpunathil
Copy link
Member

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.

@trustidkid
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants