Learn how to troubleshoot and fix the common Heroku error "Web process failed to bind to $PORT within 60 seconds of launch" in your Node.js application.
When deploying Node.js applications to Heroku, you might encounter the error "Web process failed to bind to $PORT within 60 seconds of launch" or "Error R10 (Boot timeout)." This error occurs when your application cannot listen for requests on the assigned port within the given time. Heroku dynamically assigns ports to applications and stores the assigned port in the process.env.PORT
environment variable. To resolve this, you must configure your application to use the dynamically assigned port. This article provides a step-by-step guide on how to correctly configure your Node.js application to use the process.env.PORT
variable, ensuring a successful deployment on Heroku.
Understanding the Error:
Heroku's Dynamic Port Assignment:
3000
) for your app.process.env.PORT
environment variable.Solution: Using process.env.PORT
Retrieve the port:
const port = process.env.PORT || 3000; // Use Heroku's port or default to 3000 locally
Start your server:
const express = require('express');
const app = express();
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Explanation:
process.env.PORT
to get the port assigned by Heroku.process.env.PORT
is not defined (like when running locally), we default to port 3000
.app.listen(port, ...)
function starts your server and makes it listen on the specified port.Additional Tips:
heroku logs --tail
) to pinpoint the exact cause of the error.package.json
file and installed correctly.Example (Express.js):
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello from Heroku!');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
By following these steps, you instruct your Node.js app to correctly use the dynamically assigned port on Heroku, preventing the "Web process failed to bind to $PORT" error.
This JavaScript code creates a simple web server using Express. It defines a route that responds with "Hello from Heroku!" when accessed. The server dynamically determines the port to run on, using the environment variable PORT if available (like on Heroku), otherwise defaulting to port 3000. The code includes instructions for running the server both locally and on Heroku.
const express = require('express');
const app = express();
// Get the port from the environment variable or default to 3000
const port = process.env.PORT || 3000;
// Simple route to test the app
app.get('/', (req, res) => {
res.send('Hello from Heroku!');
});
// Start the server and listen on the specified port
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Explanation:
express
library.process.env.PORT || 3000
to get the port. This line checks if process.env.PORT
is defined (which it will be on Heroku) and uses that value. If not, it defaults to port 3000
for local development./
) that sends a "Hello from Heroku!" message when accessed.app.listen(port, ...)
to start the server. It takes the port
as the first argument and a callback function that executes once the server is listening. The callback function simply logs a message to the console indicating the port the server is running on.How to Run:
Locally:
.js
file (e.g., server.js
).node server.js
.http://localhost:3000
.On Heroku:
package.json
file (if you don't have one) using npm init -y
.express
as a dependency: npm install express --save
.Procfile
(without any file extension) in your project's root directory and add the following line: web: node server.js
(replace server.js
with your file name).git init
.git add .
and git commit -m "Initial commit"
.heroku create
.git push heroku master
.heroku open
.This example demonstrates the essential steps to ensure your Node.js app correctly binds to the port assigned by Heroku, preventing the common "Web process failed to bind to $PORT" error.
Procfile
tells Heroku how to start your app. The line web: node server.js
specifies that the "web" process should be started using the node server.js
command.||
: Instead of process.env.PORT || 3000
, you can use the nullish coalescing operator (??
) if you need to differentiate between 0
and undefined
: process.env.PORT ?? 3000
.console.log
statements to check values at different points in your code and help identify the issue.This article provides a solution for the common Heroku deployment error: "Web process failed to bind to $PORT."
Problem: Heroku dynamically assigns ports to applications, preventing hardcoded port usage (like 3000
). Failure to use the assigned port results in the error.
Solution:
process.env.PORT
to fetch the Heroku-assigned port.3000
) if process.env.PORT
is undefined (for local development).app.listen(port, ...)
in Express.js).Example (Express.js):
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Additional Tips:
By understanding Heroku's dynamic port assignment and correctly configuring your Node.js application to utilize process.env.PORT
, you can avoid the "Web process failed to bind to $PORT" error. Remember to provide a fallback port for local development and always consult Heroku logs for detailed debugging information. Using these strategies ensures your Node.js applications deploy and run smoothly on the Heroku platform.