Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploying to Heroku using Node.js not working? #627

Open
NuroDev opened this issue Feb 11, 2017 · 7 comments
Open

Deploying to Heroku using Node.js not working? #627

NuroDev opened this issue Feb 11, 2017 · 7 comments
Labels
Milestone

Comments

@NuroDev
Copy link

NuroDev commented Feb 11, 2017

Been attempting to get a web app running on Heroku using NodeJS.
Used the provided recipe for setting it up: node-heroku .
However, the app builds correctly on Heroku, but when visiting the app page, it returns an application error.
Tested it with the configuration that I used and base configuration of all config options enabled(BDD & TDD). Still same issue.
Is it an issue with Heroku or the generator?

@NuroDev NuroDev changed the title Deploying to Heroku using Node.js doesn't work. Deploying to Heroku using Node.js not working? Feb 21, 2017
@NuroDev
Copy link
Author

NuroDev commented Feb 21, 2017

If more information is required, link to repository can be found here: github.com/Meadowcottage/KaplanSays. The dist files was made in the order provided by the deploying to heroku using node.js recipe.

I have checked the Heroku logs and the crash log returns the following:

2017-02-21T22:12:25.394968+00:00 heroku[web.1]: Starting process with command `npm start`
2017-02-21T22:12:28.366684+00:00 heroku[web.1]: Process exited with status 1
2017-02-21T22:12:28.384951+00:00 heroku[web.1]: State changed from starting to crashed
2017-02-21T22:12:28.282442+00:00 app[web.1]: npm ERR! Linux 3.13.0-105-generic
2017-02-21T22:12:28.283226+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2017-02-21T22:12:28.283500+00:00 app[web.1]: npm ERR! node v7.5.0
2017-02-21T22:12:28.283773+00:00 app[web.1]: npm ERR! npm  v4.1.2
2017-02-21T22:12:28.285052+00:00 app[web.1]: 
2017-02-21T22:12:28.286543+00:00 app[web.1]: npm ERR! missing script: start
2017-02-21T22:12:28.286758+00:00 app[web.1]: npm ERR! 
2017-02-21T22:12:28.286908+00:00 app[web.1]: npm ERR! If you need help, you may report this error at:
2017-02-21T22:12:28.287072+00:00 app[web.1]: npm ERR!     <https://github.com/npm/npm/issues>
2017-02-21T22:12:28.298997+00:00 app[web.1]: 
2017-02-21T22:12:28.299253+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-02-21T22:12:28.299393+00:00 app[web.1]: npm ERR!     /app/npm-debug.log

I double checked the dist/package.json and a start script is present.
Any ideas why this error may be occurring?

@silvenon
Copy link
Member

I don't use Heroku, but are you sure that package.json got deployed to Heroku? Is there any way to see that?

@NuroDev
Copy link
Author

NuroDev commented Feb 22, 2017

I have been looking into this more and I found the issue. The Heroku app by default tries to build from the root directory. Makes sense about the no script error now 😂.
Need to find a way to point Heroku to build from a sub directory or set the build directory.
If I do i'll make a PR.

@silvenon
Copy link
Member

silvenon commented Feb 22, 2017

If you see the very end of that recipe, it says that you should deploy only dist, so I'm guessing that should be your root. Is that possible to do?

@NuroDev
Copy link
Author

NuroDev commented Feb 22, 2017

I don't have a huge total use time with Heroku, but from what I have been working out is that you can instead point it to a branch to deploy from.
So best bet is to create a deployment branch and point Heroku to that branch to build from.

@silvenon
Copy link
Member

Yeah. In any case it appears that modifications to the recipe should be made. If you'd like to submit a PR once you get it working, that would be great. The simpler, the better. 😉

@silvenon silvenon added the docs label Feb 22, 2017
@UlisesGascon UlisesGascon added this to the v4.0.0 milestone Oct 28, 2019
@yuvashrikarunakaran
Copy link

Port Configuration
Heroku dynamically assigns a port for the app to use, and it must listen on that port. Ensure your app is using the process.env.PORT variable to determine the port.

Solution: Update your server.js or equivalent file to listen on process.env.PORT, like this:

js
Copy code
const PORT = process.env.PORT || 3000; // Fallback to 3000 locally
app.listen(PORT, () => {
console.log(Server running on port ${PORT});
});
2. Missing Start Script in package.json
Heroku requires a start script in the package.json file to know how to start your app.

Solution: Ensure your package.json file includes:

json
Copy code
"scripts": {
"start": "node server.js"
}
Replace server.js with your entry point if different (e.g., app.js).

  1. Incorrect Procfile
    If you’re using a Procfile, it should contain a command to start your Node.js application. If it’s missing or misconfigured, the app may not start correctly.

Solution: Create or verify the Procfile file in your root directory and include this:

makefile
Copy code
web: node server.js
Again, replace server.js with the actual entry point to your app.

  1. Uncaught Errors in the App
    There might be uncaught errors or exceptions in your app that only occur in production or Heroku’s environment. These may not appear during local testing. For example, accessing files, databases, or external APIs could fail due to incorrect configurations.

Solution: Check your Heroku logs to identify any uncaught errors. Run the following in your terminal:

bash
Copy code
heroku logs --tail
Look for any specific errors or stack traces that might point to a configuration issue.

  1. Environment Variables
    If your app depends on certain environment variables (e.g., API keys, database URLs), they might not be set correctly on Heroku.

Solution: Verify that all required environment variables are properly set in Heroku. You can set them using:

bash
Copy code
heroku config:set VARIABLE_NAME=value
6. Node Version Mismatch
Heroku might be using a different version of Node.js than you expect. If your app relies on specific Node.js versions, ensure they are specified in the package.json file.

Solution: In package.json, specify the required Node.js version like this:

json
Copy code
"engines": {
"node": "16.x"
}
Replace "16.x" with the version your app requires.

  1. Heroku Buildpacks
    If you’re using multiple buildpacks or relying on custom buildpacks, this might cause issues. Heroku automatically detects Node.js apps, so check your buildpack setup if you’ve customized it.

Solution: If necessary, set the Node.js buildpack explicitly by running:

bash
Copy code
heroku buildpacks:set heroku/nodejs
8. Database Configuration (If Applicable)
If your app connects to a database (e.g., Postgres), ensure that:

The database connection string is correctly set via environment variables.
The app handles connection errors gracefully.
Final Step: Heroku Logs
If none of the above helps, always refer to the Heroku logs. They will give more insight into the exact error the app is facing. Use the following command to tail logs in real-time:

bash
Copy code
heroku logs --tail
This will display any issues that arise when your app is deployed or accessed. The logs should help you pinpoint where the error is happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants