Asked  7 Months ago    Answers:  5   Viewed   157 times

I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn't let me write so much code, so I would just say that the running the code locally as well within my network shows no issue.

 var http = require('http');
 var fs = require('fs');
 var path = require('path');

 http.createServer(function (request, response) {

    console.log('request starting for ');

    var filePath = '.' + request.url;
    if (filePath == './')
        filePath = './index.html';

    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
        case '.css':
            contentType = 'text/css';

    path.exists(filePath, function(exists) {

        if (exists) {
            fs.readFile(filePath, function(error, content) {
                if (error) {
                else {
                    response.writeHead(200, { 'Content-Type': contentType });
                    response.end(content, 'utf-8');
        else {


 console.log('Server running at');

Any idea ?



Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku adds the port to the env, so you can pull it from there. Switch your listen to this:

.listen(process.env.PORT || 5000)

That way it'll still listen to port 5000 when you test locally, but it will also work on Heroku.

You can check out the Heroku docs on Node.js here.

Tuesday, June 1, 2021
answered 7 Months ago

After lots of googling I decided to npm install express and add

var express = require('express');
var app     = express();

app.set('port', (process.env.PORT || 5000));

//For avoidong Heroku $PORT error
app.get('/', function(request, response) {
    var result = 'App is running'
}).listen(app.get('port'), function() {
    console.log('App is running, server is listening on port ', app.get('port'));

This fixed the error, even though I don't like adding express just to avoid one error. If someone finds a better solution, please let me know.

Sunday, July 18, 2021
answered 5 Months ago

Try using this:

String host = "";
int port = System.getenv("PORT");

On Heroku, you must bind to and use the port assigned to your app, which is contained in the $PORT environment variable.

From the client, you will not need to specify a port, so only wss:// should be used (not the one with 5000).

Wednesday, August 4, 2021
answered 4 Months ago

Heroku runs in a production environment by default so it does not install the dev dependencies.

If you don't want to reinstall nodemon as a dependency, which I think you shouldn't, because it's right place is in devDependencies, not in dependencies...

Instead, you can create a second npm script, in your package.json, to avoid this error by running nodemon only in your localhost:

"scripts": {
    "start": "node fileName.js",
    "start:dev": "nodemon fileName.js"

And when you want to run the project locally, just run in your terminal npm start:dev and it will load fileName.js with nodemon.

While in Heroku, npm start runs by default and loads fileName.js from a normal node command and you get rid of that error.

2019-05-08T18:13:40.319989+00:00 heroku[web.1]: State changed from crashed to starting  
2019-05-08T18:13:41.000000+00:00 app[api]: Build succeeded  
2019-05-08T18:13:42.658048+00:00 heroku[web.1]: Starting process with command npm start  
2019-05-08T18:13:44.644005+00:00 app[web.1]: 
2019-05-08T18:13:44.644025+00:00 app[web.1]: > app@1.0.0 start /app  
2019-05-08T18:13:44.644027+00:00 app[web.1]: > node fileName.js  
2019-05-08T18:13:44.644028+00:00 app[web.1]:   
2019-05-08T18:13:45.158694+00:00 app[web.1]: app is running on port 33333  
2019-05-08T18:13:46.293205+00:00 heroku[web.1]: State changed from starting to up  
2019-05-08T18:13:47.788861+00:00 heroku[router]: at=info method=GET path="/" request_id=hidden fwd="ip" dyno=web.1 connect=0ms service=11ms status=200 bytes=245 protocol=https

I made this post in hopes to help you avoid the time it took me to debug this issue.

Monday, September 20, 2021
answered 3 Months ago

Change your Procfile to this:

web: node app.js

Being app.js the entry point to your app.

This is assuming you don't really need nodemon in your app, since you have listed it at the package.json sample you provided.

Tuesday, October 12, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :