You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用Express部署Node.js应用到Heroku并解决R10超时错误

Fixing Heroku R10 Boot Timeout for Non-Express Node.js Apps

Hey there! That R10 error is super common when deploying Node.js apps to Heroku, and you’re right—it’s almost always about how your app handles the port. Even without Express, the fix is straightforward once you know what’s going on.

Why This Happens

Heroku doesn’t let you hardcode a port like 3000 or 8080. Instead, it dynamically assigns a port to your app and passes it to your code via the $PORT environment variable. If your app doesn’t bind to this exact port within 60 seconds of launching, Heroku kills the process and throws that R10 error.

The Fix: Use the PORT Environment Variable

You just need to adjust your Node.js code to listen on the port provided by Heroku, with a fallback for local development. Here’s how to do it with Node’s built-in http module (the same logic applies to other non-Express setups):

Before (Hardcoded Port - Causes Error)

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World!\n');
});

// Hardcoded port - bad for Heroku
server.listen(3000, () => {
  console.log('Server running on port 3000');
});

After (Uses Heroku's Port - Fixes Error)

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World!\n');
});

// Use Heroku's PORT env var, or 3000 for local dev
const port = process.env.PORT || 3000;
server.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

Double-Check Your Procfile

Make sure your Procfile (in the root of your project, no file extension) is set up correctly to start your server. It should look like this:

web: node your-server-filename.js

Replace your-server-filename.js with the actual name of your main server file. If you don’t have a Procfile, Heroku will try to use the start script in your package.json—so you can also add this to your package.json if you prefer:

"scripts": {
  "start": "node your-server-filename.js"
}

Test Locally First

To make sure your code works with dynamic ports, test it locally by setting the PORT environment variable manually:

PORT=5000 node your-server-filename.js

Then visit http://localhost:5000—if it loads, you’re good to go.

That’s all there is to it! Push these changes to Heroku, and your app should start up without the R10 timeout.

内容的提问来源于stack exchange,提问作者Joji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:04:10