Skip to content

node.js express permission error on linux

using arch with admin user account and no sudo on this script:

var express = require('express');

var fs = require('fs');

var app = express();
app.get('/lol', function(req, res) {
    res.sendFile('second.html', {root: __dirname })
});
var port = process.env.PORT || 81;
var server = app.listen(port);

i get this error that didnt change when i changed the port or the url to trigger it it instantly gives me this error after i run the command: node site.js (the code above is site.js)

    node:events:371
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES: permission denied 0.0.0.0:81
    at Server.setupListenHandle [as _listen2] (node:net:1302:21)
    at listenInCluster (node:net:1367:12)
    at Server.listen (node:net:1454:7)
    at Function.listen (/home/{name}/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/home/{name}/Documents/web/demo/site.js:10:18)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1346:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EACCES',
  errno: -13,
  syscall: 'listen',
  address: '0.0.0.0',
  port: 81
}

if i run the script with sudo it works fine but i dont want to run it in sudo beacuse i have to run it on a server with no sudo. any help?

Answer

Using port below 1024 without root permission is common issue. Try port bigger than 1024.