Setting up process.env variables using EXPORT while running node with sudo
Asked Answered
W

3

19

I'm using node.js on EC2

I type

EXPORT PORT=80

in terminal, and i see that it correctly saves it when i type EXPORT

But when I run my node.js app with the following:

...
console.log(process.env);
...

PORT is not listed in the object when I'm running it with sudo:

sudo node app.js

How do I set PORT so that I can access it from the process.env object while running node with sudo?

Wits answered 11/1, 2013 at 12:16 Comment(0)
B
25

To set process.env variable use the following code:

sudo PORT=80 node server.js

Of course, you can set multiple process.env variables:

sudo PORT=80 HOST=localhost node server.js

Normally, EXPORT should work too. But sudo creates its own environments and then starts your program as root. So, you shall either add PORT to sudo's environment or force it to preserve your own environment.

To change sudo's environment you shall modify /root/.profile.

To force it to preserve your own environment use -E key:

sudo -E node app.js
Bosket answered 11/1, 2013 at 12:26 Comment(10)
Is there no way to set it up permanently so I don't have to set them up every time I run the server?Wits
EXPORT should work too. It works for me, though I prefer not to change the global environment.Bosket
If you want to configure your environment for production then its better to write simple upstart script.Bosket
Yes, I've been using both a forever script and setting it up every time I run node. But I want to change the global env to include PORT. EXPORT should work, but it's not for me. I'm trying to figure out why.Wits
If export PORT=8080 && node -e "console.log(process.env.PORT);" prints undefined for you, then I have no idea why its happening. If it prints 8080, then include more details about the way you starting your script and I'll try to help you.Bosket
Ah. I think I identified the problem, but don't know how to solve it. I got PORT=8080 working. But I'm actually trying to get it to work on PORT=80. So I need to do export PORT=80, sudo node app.js, which causes process.env.PORT to be undefined. Any advice on how to get PORT 80 to work?Wits
Are you sure that no other applications are already listening to port 80? If you have ngnix or appach installed then port 80 may be already taken. Are you sure that your problem is with setting process.env.PORT and not with listening to port 80?Bosket
The thing is, when I do sudo PORT=80 node app.js, it runs perfectly on PORT 80. Only when I do export PORT=80 and then run sudo node app.js separately is where the problem occurs. Maybe it's some sort of permissions issue?Wits
Ok, now I figured out your problem. Sudo creates its own environments and then starts you program as root. So, you shall either add PORT to sudo's environment or force it to preserve your own environment. To change sudo's environment you shall modify /root/.profile. To force it to preserve your own environment use -E key (e.g. sudo -E node app.js).Bosket
I edited your question and my answer to reflect the real problem and appropriate solution for this problem.Bosket
N
0

I know it is an old post but I have the same permission problem running node.js on port 80. I made a workaround to avoid running with sudo and having to define the PORT in node run command (sudo PORT=80 node server.js). What I did was redirect the traffic for the PORT 80 to another allowed port, in my case 3000.

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
Nidify answered 6/12, 2020 at 19:19 Comment(0)
B
0

you can use cross-env to set up env and add to your scripts. eg.

scripts: {
"build": "cross-env PORT=3000 <any_command>"
}

You can also set multiple variables . eg.

scripts: {
    "build": "cross-env PORT=3000 NODE_ENV=production <any_command>"
    }

Process this using process.env.PORT and process.env.NODE_ENV

Bouie answered 6/9, 2022 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.