Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variable in EC2 Instance for a node app

I'm currently running a node application on an EC2 Instance Linux using PM2 to manage and it keeps crashing, throwing an error that reads Username is required.

This username is being fed from process.env.USERNAME that I set using dotenv for development.

So far using AWS shell I have tried to pass the env variable in the following paths:

/etc/profile
/etc/bashrc
/etc/profile.d
~/.bash_profile
~/.bashrc

The variables were put in this form: export VARIABLE_KEY=VALUE

I also tried to put the variables inside /etc/environment. None of which is making it work.

I restarted the server each time, logged out, and logged back in.

like image 330
Victor O'Frank Avatar asked Mar 23 '26 02:03

Victor O'Frank


1 Answers

The way I solved it was to restart pm2 using pm2 restart --update-env. When you first run pm2 start, it caches the env variables, so I needed to restart it to add the newly created env variables.

like image 54
Victor O'Frank Avatar answered Mar 24 '26 14:03

Victor O'Frank