13

Deploying on Heroku with Node.js using Mac

My issue:

State changed from starting to crashed &&  
sh: 1: nodemon: not found &&  
Failed at...start script &&
status 1...code=H10

After creating my front-end, with React, back-end server, with node.js/express.js, and database, with PostgreSQL, I attempted to deploy my server on Heroku with Git. Since I already had Git, I moved onto Heroku CLI

First, from the terminal in my server...

brew install heroku/brew/heroku
heroku create
git remote -v
git push heroku master

If this is not your first time using Heroku...

heroku git:remote -a theUrlYouWant
git push heroku master

...otherwise...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:

app.listen(process.env.PORT || 3000, () => {  
  console.log(`app is running on port ${process.env.PORT}`);
})  

...if you added port:

git add .
git commit -m "adding port"
git push heroku master

...finally, from my terminal in server:

➜ folderName git:(master) heroku open  
➜ folderName git:(master) heroku logs --tail

2019-05-08T18:07:23.253827+00:00 heroku[web.1]: Starting process with command npm start  
2019-05-08T18:07:25.323748+00:00 heroku[web.1]: State changed from starting to crashed  
2019-05-08T18:05:17.074233+00:00 app[web.1]: > nodemon fileName.js  
2019-05-08T18:05:17.074235+00:00 app[web.1]:   
2019-05-08T18:05:17.098124+00:00 app[web.1]: sh: 1: nodemon: not found  
2019-05-08T18:05:17.102512+00:00 app[web.1]: npm ERR! file sh  
2019-05-08T18:05:17.102801+00:00 app[web.1]: npm ERR! code ELIFECYCLE  
2019-05-08T18:05:17.103068+00:00 app[web.1]: npm ERR! errno ENOENT  
2019-05-08T18:05:17.103239+00:00 app[web.1]: npm ERR! syscall spawn    
2019-05-08T18:05:17.104259+00:00 app[web.1]: npm ERR! app@1.0.0 start: nodemon fileName.js  
2019-05-08T18:05:17.104361+00:00 app[web.1]: npm ERR! spawn ENOENT  
2019-05-08T18:05:17.104553+00:00 app[web.1]: npm ERR!  
2019-05-08T18:05:17.104692+00:00 app[web.1]: npm ERR! Failed at the app@1.0.0 start script.  
2019-05-08T18:05:17.104841+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above. 

[...]

2019-05-08T18:05:17.171915+00:00 heroku[web.1]: Process exited with status 1  
2019-05-08T18:05:37.338695+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=yourURL.herokuapp.com request_id=hidden fwd="ip" dyno= connect= service= status=503 bytes= protocol=https  
Community
  • 1
  • 1
Q.McKinley
  • 551
  • 1
  • 4
  • 11
  • 2
    Hi! It's encouraged on SO to write questions you answer yourself, so thanks for doing this! However, you should make the question and answer separate. So please remove the answer part of the question, and then post it as an answer ... to this question! Thanks, and welcome to SO :) – Max von Hippel May 08 '19 at 21:22

4 Answers4

30

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="/" host=yourURL.herokuapp.com 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.

Q.McKinley
  • 551
  • 1
  • 4
  • 11
  • 2
    Perfect! Now once the (I think 10 hour?) time period passes after which you are allowed to accept your answer, remember to do so. – Max von Hippel May 09 '19 at 05:01
  • a little update, Heroku does install dev dependencies, but pruning it after the build; so you can use your dev dependencies to build your app; i.e: "scripts":{"install": "babel src/app.js --out-file build/app.js"} – 2oppin Aug 11 '21 at 13:03
  • i got some error still not fixed. – Yogi Arif Widodo Aug 09 '22 at 11:33
2

Here`s what worked for me:

In your Heroku app, go to Settings, then click Reveal Config Vars and then add a new record with KEY NPM_CONFIG_PRODUCTION and Value false.

Gabriel Arghire
  • 1,992
  • 1
  • 21
  • 34
2

I added nodemon to the dependencies in package.json and it is working now.

 "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "clarifai": "^2.9.1",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "knex": "^0.95.4",
    "pg": "^8.6.0", 
    "nodemon": "^2.0.7"
  }
JoeyFlaum
  • 25
  • 5
-1

TL;DR

  • Define two commands for running the app, one for local development and one for production.

before

"start":nodemon fileName.js

After

"start":node fileName.js,

"dev":nodemon fileName.js,

To run locally run npm run dev to start the app instead of npm run start

Explained

"dependencies": Are installed in production, that means these will be in your production

"devDependencies":

  • Are for the local developement, these are not included in the production
  • "devDependencies": { "nodemon": "^2.0.21", }
  • Here nodemon is a dev dependency and is not included in your production, it makes sense since the nodemon is used to hot reload your local app.