1

I'm running a node app created with create-react-app. I wanted to make it run on port 80, so I added a .env file with the contents

PORT=80

When I run npm start, I get

? Admin permissions are required to run a server on a port below 1024.

but when I run sudo npm start, the app starts on port 3000.

Why does my .env file get ignored when I run with sudo, and how can I fix this?

EDIT: This question is not about how to keep my current environment variables when I run sudo. This question is about the behavior of .env, and why it doesn't work when running with sudo.

Interestingly, when I use su, eg.

$ su
# npm start

It runs on port 80.

To be entirely clear, I would like to know how I can run my app on port 80 without passing the port in the command, i.e. sudo PORT=80 npm start. I believe this is the whole point of .env, hence the question.

Michael Dorst
  • 6,449
  • 10
  • 35
  • 64
  • Possible duplicate of [How to keep environment variables when using sudo](https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo) Have a look at this one. – Praveen Kumar Purushothaman Apr 17 '19 at 18:16
  • @PraveenKumarPurushothaman This is related, but I want to know about the behavior of dotenv, because I would have expected node to insert `PORT=80` into whatever environment I end up in. – Michael Dorst Apr 17 '19 at 18:25

1 Answers1

0

I don't think you can run a server on port 80 without root permissions, but I think you can redirect connections from port 80 to other ports.

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000
Lazzy
  • 58
  • 6
  • This is useful, thank you, but not quite what I'm looking for. I don't mind running it as root, I just want to do it without passing the port eg. `sudo PORT=80 npm start`. – Michael Dorst Apr 17 '19 at 19:48