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?

Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
gotta have my pops
  • 878
  • 4
  • 11
  • 22

3 Answers3

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
Blaszard
  • 30,954
  • 51
  • 153
  • 233
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • 1
    Is there no way to set it up permanently so I don't have to set them up every time I run the server? – gotta have my pops Jan 11 '13 at 18:52
  • `EXPORT` should work too. It works for me, though I prefer not to change the global environment. – Leonid Beschastny Jan 11 '13 at 19:48
  • If you want to configure your environment for production then its better to write [simple upstart script](http://stackoverflow.com/a/7028694/1202461). – Leonid Beschastny Jan 11 '13 at 19:54
  • 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. – gotta have my pops Jan 11 '13 at 19:57
  • 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. – Leonid Beschastny Jan 11 '13 at 20:05
  • 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? – gotta have my pops Jan 11 '13 at 21:00
  • 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? – Leonid Beschastny Jan 11 '13 at 21:11
  • 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? – gotta have my pops Jan 11 '13 at 23:34
  • 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`). – Leonid Beschastny Jan 12 '13 at 10:09
  • I edited your question and my answer to reflect the real problem and appropriate solution for this problem. – Leonid Beschastny Jan 13 '13 at 10:19
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
dave008
  • 402
  • 3
  • 9
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

prashant
  • 210
  • 2
  • 11