0

I'm having some trouble accessing an environmental variable that I created on a linux server. I set up the variable permanently by adding: export VAR_NAME=value Into the /etc/environment file.

The variable appears when I use the printenv command but when I start the server using PM2 it doesn't exist when calling process.env.VAR_NAME.

I've tried restarting the machine, and restarting bash, and restarting the pm2 processes themselves multiple times. It seems like some instance of something needs to be restarted, because from my understanding what I wrote into the /etc/environment file gets executed on start up, so the variable isn't technically permanent, making a reboot not really do anything.

Any information on what instance needs to be restarted would help, or a different way of doing this entirely is good.

Edit
Calling process.env in the node app doesn't come up as undefined, I see all the variables listed from the app, but the variable I created isnt listed there. but it is listed when I call from the terminal

Mason
  • 3
  • 2
  • Possible duplicate of [process.env variables are undefined even after export](https://stackoverflow.com/questions/33737660/process-env-variables-are-undefined-even-after-export) – kht May 10 '19 at 00:05
  • maybe take a look https://github.com/Unitech/pm2/issues/867 – 王仁宏 May 10 '19 at 00:09

1 Answers1

0

I believe what you are looking for is this package. https://www.npmjs.com/package/dotenv

The dotenv package allows you to create a file in the root of your project titled ".env" you can add environment variables for your process within this file.

You can learn more about this from this tutorial https://www.twilio.com/blog/working-with-environment-variables-in-node-js-html

I do not believe you can "export" variables in your terminal and have them appear within the process.env object.

Travis Delly
  • 1,194
  • 2
  • 11
  • 20