When I run my nodejs application on my Ubuntu Linux server using node server.js
it works correctly, and outputs the value of the environment variable $db
using process.env.db.
However, the app breaks when running sudo pm2 start server.js
, seeing the environment variable as undefined.
I've tried adding the variable in the following files:
db="hello"
db="hello"
export db="hello"
I've also rebooted and run source ~/.bashrc
to ensure the variable is available.
I think I've tried everything mentioned here, I don't know what else to do:
You really do not need to set up your own environment to start learning Node. js. Reason is very simple, we already have set up Node.
Note that saying source ~/.bashrc
you are loading the variables on your current user. However, when you say sudo ...
you are running with the user root
, so this won't change.
What you can do is to use sudo
with -E
:
sudo -E pm2 start server.js
From man sudo
:
-E, --preserve-env
Indicates to the security policy that the user wishes to reserve their
existing environment variables. The security policy may eturn an error
if the user does not have permission to preserve the environment.
Please refer to this thread https://github.com/Unitech/pm2/issues/204
Seems like your environment variables get's cached.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With