3

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.

9
  • if i understood correctly you want to set env variable for your node application or is it for your instance? Commented Dec 19, 2020 at 5:06
  • 1
    Whatever you are experiencing would not be specific to it being an Amazon EC2 instance. It is the operating system that controls environment variables. Commented Dec 19, 2020 at 5:13
  • @JatinMehrotra Yes, I want to set the env variable for the node application. meanwhile on "printenv", the system lists all my env variables but my node app is just not picking them up when running Commented Dec 19, 2020 at 5:21
  • I have posted a solution which i follow. Commented Dec 19, 2020 at 5:27
  • 1
    @JatinMehrotra I currently have a .env file locally, I'm also running a CI/CD from gitlab to my EC2 Instance, which automatically exempts the file from being deployed to EC2. I would think that for security reasons, it might not be a good idea to commit the .env file or have it living on the server. wouldn't you think so too? Commented Dec 19, 2020 at 5:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.