Confusing Environment Variables Settings

Hi Cleavr Team,

really like your service, keep up the good work!

I just deployed a NestJS API and after hours of troubleshooting I finally got it working.

I kept getting a 502 response from the server and in the end it turned out to be the way I declared the Environment Variables.

You have prominently placed the “Environment” - option in the main menu of the Webapp section, so I went and set all my variables there.

First deployment 502 error…

I started investigating and SSH’d into the server and in the ‘latest’ folder there is the .env file with all variables.

I started the app manually with ‘npm run start:prod’ and suddenly it worked fine. So my conclusion was to check PM2 …

The Web Dashboard didn’t load any logs … (happy to report a separate bug report or am I missing something here too??) so I checked again on the server directly and indeed, the ENV vars were not found.

So the search continues, and after a while I found in the “Settings” menu, under the “Build” - Tab the option for “PM2 ECOSYSTEM”, which holds a JSON object with an attribute “env”.

I added my env vars there and voila it works like a charm.

The point why I am posting this here now is constructive feedback rather than criticism. I hope if someone else stumbles across this issue, this post might help!

@cleavr is this an intentional decision, a bug, or what is the “Environment” - option in the main menu for then? Just curious about it, because it seems like a bug to me and looking forward to your feedback!

Thanks

Hi @Hocus -

Welcome to the Cleavr forum and I’m glad that you are liking Cleavr!

But, I’m sad to hear that we didn’t make the troubleshooting and resolution journey more intuitive and that it cost you hours of troubleshooting… That’s definitely the antithesis of what we want the Cleavr experience to be and we’ll look into the journey more to see how we can make it better.

To answer this question first - this isn’t a bug and indeed is intentional, but I do see we haven’t provided good enough indicators of what the differences are.

The Environment section is meant for apps that are able to read from a .env file. This is where you can add and maintain your .env environment variables. However, if the app isn’t setup to read from a .env file, then essentially the Environment section is pointless.

Some NodeJS frameworks are packaged with a module to read from .env, others require a module to be installed if desired. For NestJS, they have the @nestjs/config that can be used to read .env files. https://docs.nestjs.com/techniques/configuration

If a web app isn’t configured to read from a .env file, then an alternative, if env variables are required, is to update the PM2 Ecosystem script that you found in webapp > settings > build. This is the PM2 config script that includes instructions for building and activating PM2 for the app as well as can include env variables that will be injected by the Node at runtime.

I’d assume that your are referencing the Webapp Logs Report. It could be that there were no logs for the web app itself. Though, I’ll check into it to make sure.

When getting a 502 error with a NodeJS-based app, I always recommend looking at the PM2 logs, which is found in multiple places in Cleavr:

In the deployment details page, there is a Load PM2 logs button

On the server > logs > PM2 log type

And on server > services > check the NodeJS heartbeat

The interesting thing about PM2 logs is that 90% of the time, the most valuable logs are found in the global PM2 logs found at /opt/pm2/pm2.log and not in the individual web app log… Typically, this will show you exactly why you are getting a 502 error with a NodeJS app.


I hope the above helps to clarify things for you as well as anyone else who stumbles upon this post. Though, we’ll be sure to look for ways to clear it up more in the app itself. :slight_smile:

Thanks again so much for your feedback!

1 Like

Thank you @amiedema !! Really appreciate the detailed answer, lots of valuable information - specifically how to find the different logs for PM2. That’s what I thought I would find under Logs Report, but I guess I still need to get more familiar with the different features and lingo used. What kind of logs will that section provide - just curious.

Yeah thats what I am using, and my app reads the .env file locally just fine. It also reads the .env file correctly when starting the app within the ‘latest’ folder manually with npm run start:prod.

It seems that only when using PM2 the .env file will be ignored and uses the PM2 config script env vars instead.

Is there a way to change this, maybe removing the env vars in the PM2 config will force the env file to work?!

Thanks again for your help!

Gotcha!

One of the solutions here I think will be of use - https://stackoverflow.com/questions/50689644/node-dotenv-wont-work-with-pm2

Let me know if you try it and if it works - then I’ll update our troubleshooting guides. :slight_smile:

I think it’s also worth pointing out somewhere that if you use an .env file + Github Actions to build your app, the env vars may not come through as Github doesn’t know about them during the build process.

I don’t know enough about Github actions to understand if there is a workaround.
So I could be wrong.

Yup… that’s a limitation when using GitHub Actions and when env variables are required for the build step.

The good news is we get you started with the workflow file so you can edit it and add tests and even add env variables / work with secrets as needed.

The first answer here looks like a solid workaround that we can include in our own documentation - https://stackoverflow.com/questions/60176044/how-do-i-use-an-env-file-with-github-actions

run: |
          cat << EOF >> .env
          API_ENDPOINT="https://xxx.execute-api.us-west-2.amazonaws.com"
          API_KEY=${{ secrets.API_KEY }}
          EOF

Great. Will have a look at that.
Thanks for the quick reply :ok_hand: