0

I created an Ubuntu micro instance on AWS, installed node and everything. I have some API credentials in my node.js app, that I'd like to store in environment variables. I'm not Linux savy, so I googled and found that best way to store these variables permanently is to edit etc/environment. I did so, and this is how it looks:

NO_OF_DAYS=30
REFRESH_INTERVAL_MINS=30
API_PASSWORD="**pppp!!"
API_USERNAME="johnny88!"

I rebooted system after this change, as I read it is needed.

Yet when I try to access these variables in my node.js app, with i.e. process.env.API_PASSWORD or process.env.NO_OF_DAYS, it just returns undefined as a value. What am I doing wrong here?

kecman
  • 813
  • 3
  • 14
  • 34
  • @shellter: `/etc/environment` is not a shell script, it only contain variable definition that will be set system wide. `export` won't work. – Cyrbil Jan 05 '16 at 14:42
  • 1
    @kecman: '/etc/environment' does not allows quoting, put the value as it: `API_USERNAME=toto` – Cyrbil Jan 05 '16 at 14:43
  • @Cyrbil : Thanks for sharing, (obviously) I didn't know that ;-) Good luck to all. – shellter Jan 05 '16 at 14:44
  • I deleted other entries and left only NO_OF_DAYS and REFRESH_INTERVAL_MINS, rebooted Ubuntu, and I still get undefined as a value of process.env.NO_OF_DAYS. And there was already an entry with quotes, some PATH=".....", so it should work with quotes? – kecman Jan 05 '16 at 15:09
  • I just have realized that when I call my node.js app with "node app.js" it gets the value as it should, and I used to run it with "sudo node app.js" – kecman Jan 05 '16 at 15:13
  • Do you know why this is? having a similar problem – random_0620 Jun 05 '17 at 03:12
  • @s_kirkiles I guess because sudo uses different enviroment variables, and I set my environment variables without sudo, so I have to run app.js with just "node app.js" instead of "sudo node app.js" – kecman Jun 06 '17 at 00:53
  • 1
    Possible duplicate of [Node Server on Ubuntu can't access environment variables as sudo](https://stackoverflow.com/questions/22690644/node-server-on-ubuntu-cant-access-environment-variables-as-sudo) – Andrew Jul 24 '18 at 23:45

0 Answers0