DEV Community

Alex Aslam
Alex Aslam

Posted on

Debug Heroku Deployment Errors: Slay Asset Compilation & Env Vars Dragons πŸ‰

You just typed git push heroku main with confidence.

Then... BOOM.

ERROR: Asset compilation failed or ENV_VAR not set.

Sound familiar? Let’s fix these deployment gremlins once and for all.


1. Asset Compilation: The Silent Killer (in Rails)

Why it fails:

  • Missing dependencies (e.g., yarn not installed)
  • Outdated Node/Ruby versions
  • Path issues in deployment environment

The Fix:

# Precompile assets locally before deploying:  
RAILS_ENV=production bundle exec rails assets:precompile  

# Ensure Node.js & Yarn in deployment:  
heroku buildpacks:add --index 1 heroku/nodejs  
heroku buildpacks:add --index 2 heroku/ruby  
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Add to package.json:

{  
  "scripts": {  
    "postinstall": "rails assets:precompile"  
  }  
}  
Enter fullscreen mode Exit fullscreen mode

2. Environment Variables: The "It Works on My Machine" Trap

Classic Errors:

  • Forgetting to set RAILS_ENV=production
  • Typo in variable name (SECRET_KEY_BASE vs SECRET_KEY_BAS)
  • Not restarting dynos after setting vars

The Fix:

# Set and VERIFY:  
heroku config:set RAILS_ENV=production SECRET_KEY_BASE=$(rake secret)  
heroku config:get SECRET_KEY_BASE  # Double-check!  
heroku restart  
Enter fullscreen mode Exit fullscreen mode

Debugging Pro Move:

# config/initializers/env_check.rb  
raise "SECRET_KEY_BASE missing!" if ENV["SECRET_KEY_BASE"].nil?  
Enter fullscreen mode Exit fullscreen mode

3. Other Deployment Demons 😈

Database Setup Fail

Symptom: PG::ConnectionBad

Fix:

heroku run rails db:migrate  # ← Often forgotten!  
Enter fullscreen mode Exit fullscreen mode

File Permission Nightmares

Symptom: EACCES: permission denied

Fix:

# In Dockerfile:  
RUN chown -R app:app /app  
Enter fullscreen mode Exit fullscreen mode

Memory Limits (Asset Compilation)

Fix:

# Heroku  
heroku config:set NODE_OPTIONS="--max_old_space_size=2048"  
Enter fullscreen mode Exit fullscreen mode

4. Your Deployment Survival Kit

  1. Logs Are Oxygen:
   heroku logs --tail # Live view  
Enter fullscreen mode Exit fullscreen mode
  1. Reproduce Locally:
   RAILS_ENV=production bundle exec rails assets:precompile  
Enter fullscreen mode Exit fullscreen mode
  1. Staging Environment:
   heroku create my-app-staging --remote staging  
Enter fullscreen mode Exit fullscreen mode

Real War Story

Last week, I spent 3 hours debugging why CSS wasn’t loading.

Cause: Precompilation failed silently.

Solution:

heroku run rails assets:clean # Cleared cached garbage  
Enter fullscreen mode Exit fullscreen mode

Your Turn

Next time deployment fails:

  1. Don’t panic
  2. Check logs
  3. Verify assets + env vars
  4. Slay the dragon πŸ”₯

Got a deployment horror story? Share below! πŸ‘‡

Top comments (1)

Collapse
 
parag_nandy_roy profile image
Parag Nandy Roy

This is πŸ”₯