gotta have my pops gotta have my pops - 1 year ago 102
Node.js Question

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

I'm using node.js on EC2

I type


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:


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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download