Heroku deployment stuck on port listening, Please help

Hi all,
I have a very basic express server setup using node. Works fine locally using Heroku local web. The problem occurs after it is deployed to Heroku. It appears to be stuck on the build phase where it listens to the port indefinitely and timeout using the nodejs default buildpack.

my file structures are:
-public
-css
-style.css
-index.html
-index.js
-package.json
-Procfile

Index.js

const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;
const app = express();
app
	.use(express.static(path.join(__dirname, 'public')))
	.get('/', (req, res) => res.send(path.join(__dirname, 'public', 'index')))
	.listen(PORT, () => console.log(`listening on port <<${PORT}>> ----`));

package.json

{
	"name": "hello-world-express-static",
	"version": "1.0.0",
	"description": "express server used to render html hello world page with use of JS",
	"main": "index.js",
	"scripts": {
		"start": "node index.js",
		"test": "mocha ./test",
		"build": "node index.js",
		"deploy": "node index.js"
	},
	"keywords": [ "node", "heroku", "express" ],
	"devDependencies": {
		"mocha": "^8.1.3",
		"supertest": "^4.0.2"
	},
	"dependencies": {
		"body-parser": "^1.19.0",
		"cors": "^2.8.5",
		"dotenv": "^8.2.0",
		"express": "^4.17.1"
	},
	"engines": {
		"node": "12.x"
	},
	"repository": {
		"type": "git",
		"url": "https://github.com/winstoncwang/Sandbox/tree/master/hello-world-express-static"
	}
}

Procfile

web: node index.js

Build log

-----> Node.js app detected
       
-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       NODE_VERBOSE=false
       
-----> Installing binaries
       engines.node (package.json):  12.16.2
       engines.npm (package.json):   6.14.4
       
       Resolving node version 12.16.2...
       Downloading and installing node 12.16.2...
       npm 6.14.4 already installed with node
       
-----> Installing dependencies
       Installing node modules
       
       > nodemon@2.0.3 postinstall /tmp/build_9cef07c9_/node_modules/nodemon
       > node bin/postinstall || exit 0
       
       Love nodemon? You can now support the project via the open collective:
        > https://opencollective.com/nodemon/donate
       
       added 198 packages in 3.344s
       
-----> Build
       Running build
       
       > ecommerce-project@1.0.0 build /tmp/build_9cef07c9_
       > node index.js
       
       The app is running on port: 5000
-----> Timed out running buildpack Node.js
Terminated
 !     Push failed

Link to my Github repo:
[https://github.com/winstoncwang/Sandbox]

Thank you for taking the time to go through all of this. Any help is appreciated.

1 Like

Hey Winston,

nice to meet you! :wave:

Why do you have these two lines in your package.json?

"build": "node index.js",
"deploy": "node index.js"

Maybe Heroku actually runs the build script, because you have one.

build should be something like npm install ...


In general,

how do you try to deploy it?
Do you build locally and use the heroku cli?
Or do you push to GitHub and fetch from there?

1 Like

Hi Miku,

Thank you so much for your advice. It works now.

I think I am not familiar with the deployment process. Using node index.js does run the script instead of building one as you said. I changed the script to npm install, and the Procfile to ensure Heroku was running the build script. And the deployment was successful.

I tend to run ‘npm install’ locally before starting the server. Yes, I do use Heroku CLI and push from there.

So the take away for me is that Heroku receives files pushed, packages are installed using npm install rather than automatically by Heroku?

Great work :+1:

I think about like this:
When you have an application on your machine, e.g. a Node/Express server, a React frontend, and want to run it, you need a runtime, e.g. Node, on your machine. You have to set it up, you have to download packages, you have to create a build, you have to run it

Heroku basically gives you sort of a machine (a containered Heroku Dyno), where you can do that stuff in a professional and managed fashion instead of using your own machine. When you connect it to an online git repository, Heroku only has your package.json, not the node_modules, so you have to tell it what to do now. Not everyone wants to install packages everytime (sometimes they are cached), sometimes you want to do other stuff, testing etc. You have the freedom to decide what you want to do, that’s why you have to give Heroku some commands.