部署Strapi至Heroku时遭遇H10(应用崩溃)错误求助
Hey there, let's troubleshoot that H10 "App crashed" error you're hitting with your Strapi deployment on Heroku. This error usually means Heroku can't successfully start or connect to your app instance, so let's break down the most common fixes step by step:
1. Verify Your Port Configuration
Heroku assigns a dynamic port to your app via the PORT environment variable, so hardcoding a port like 1337 will cause Strapi to fail to bind to the correct port. Check your config/server.js file (for Strapi 3.x) to ensure it's using the environment variable:
module.exports = ({ env }) => ({ host: env('HOST', '0.0.0.0'), port: env.int('PORT', 1337), // Falls back to 1337 only if PORT isn't set });
Heroku automatically sets the PORT variable, so as long as your config reads it, this should resolve port-related binding issues.
2. Fix Database Configuration (Critical for Production)
Strapi's default SQLite database doesn't work on Heroku—Heroku's ephemeral file system means your database will be wiped on dyno restarts, and it can even prevent your app from starting entirely. Here's what to check:
- Add the PostgreSQL Add-on: Make sure you've added Heroku Postgres to your app (you can do this via the Heroku Dashboard or with
heroku addons:create heroku-postgresql:hobby-dev). - Update Database Config: Modify
config/database.jsto use Heroku's auto-generatedDATABASE_URLand enable SSL (required for Heroku Postgres):
module.exports = ({ env }) => ({ defaultConnection: 'default', connections: { default: { connector: 'bookshelf', settings: { client: 'postgres', uri: env('DATABASE_URL'), }, options: { ssl: env.bool('DATABASE_SSL', true), }, }, }, });
3. Check Your App's Startup Logs (Most Important Step!)
The logs you shared only show the router failing to connect—we need to see why your app crashed during startup. Run this command in your terminal to get real-time logs:
heroku logs --tail
Look for errors like:
- Failed database connections
- Missing dependencies (check if
package-lock.jsonoryarn.lockwas committed to Git—Heroku needs this to install consistent dependencies) - Syntax errors in your code or config files
- Missing environment variables (like
NODE_ENV=production—Heroku sets this by default, but double-check withheroku config)
4. Confirm Your Startup Command
Heroku uses the start script in your package.json to launch your app. Ensure it's set to run Strapi in production mode:
"scripts": { "start": "strapi start", "develop": "strapi develop", "build": "strapi build", "strapi": "strapi" }
If you're using a custom build step, make sure it's running correctly (e.g., strapi build should run before starting in production).
5. Ensure All Critical Files Are Committed
Double-check that your Git repo includes:
config/folder (all your Strapi configuration files)package.jsonand your lock file (package-lock.jsonoryarn.lock)- Any custom code or extensions you've added
Missing these files will cause Heroku to fail to set up or start your app.
Start with checking the detailed startup logs—they'll give you the exact error that's crashing your app, which is the fastest way to resolve this. Let me know what you find!
内容的提问来源于stack exchange,提问作者Kisho

