App works locally but crashes on Heroku [Solved]

App works locally but crashes on Heroku [Solved]
0.0 0


I’ve written my image search API.
I wrote it using Atom and running it in chrome (localhost:).
Everything works fine locally but crashes when deployed to heroku.
I’ve changed the port that I’m listening to from 3000 to process.env.PORT. I’ve also gone from using dotenv (.env) file to host the environmental variables to plugging them into the config vars under the ‘settings’ tab in the heroku app.

When I try to run the app, I get the following error:

‘Application Error:
An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details.’

The heroku log give this error:
‘2016-09-12T02:27:01.771091+00:00 heroku[router]: at=error code=H10 desc=“App crashed” method=GET path="/" request_id=5285b28a-ae99-4f38-a043-d3be52e4b1d7 fwd=“” dyno= connect= service= status=503 bytes=’

My code can be found here:

At this point, I’ve spent more time searching for an API to use for the search and trying to deploy to heroku than writing the app!! Pretty frustrating!

Any help greatly appreciated…


I set the port a little differently - see if this works:

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

// Start node server
app.listen( app.get( 'port' ), function() {
  console.log( 'Node server is running on port ' + app.get( 'port' ));


Jackson, thanks for the reply. That’s the port configuration I’ve used in the first 3 api apps, so doubtful that’s the problem, but I’ll give it a shot anyway.


Just taking a guess, since that’s the first clear difference I saw between our approaches.

I’m not used to using Mongoose. Do you need to require Mongo as well, or will Mongoose run fine without it? You haven’t got Mongo installed in this package, so that could be throwing your server error. (I figured that might be the issue, since you probably have mongo installed globally on your local machine, but Heroku doesn’t know you want it in this package…)


I just checked your previous projects - they have mongoose without mongo, too, so that’s unlikely to be it.

Do you have any other logs that provide more detail?


Not really… That’s the really frustrating part. I’m not that familiar with Heroku so I’m just going off the ‘view logs’ link. Because it works locally, I can’t try to debug on my system then push a working version, so I’m trying make changes and pushing each change to GitHub, then testing on Heroku. There has to be a better way.

I have no idea what it could be. The only other idea I have is hardcoding the env variables into the code, then testing that… I’d rather not expose that info on github, but it’s not that critical… Just a bing key and info to my test database… Still, would rather avoid that if possible considering this is a learning experience and need to figure this out for the long run anyway.

I didn’t use dotenv on the last API that required a database, and hard coded that info and that app which worked in heroku. That’s why I’m tending toward that being the problem,. But it seems pretty straightforward to include the variables in the config vars section (which I did). I even copied and pasted from original so as to avoid typos…


I had some issues with dotenv and Heroku in my last project too. I did the same thing: lots of tiny pushes to Heroku master, and it does feel like there should be a better way! I feel your pain - I have wasted a lot of time on problems like this!!!

Have you tried console.logging your env variables just to check them? And typeof checking them as well?

You could try hard coding them as a sanity check, and then change the keys. Bing will generate new ones for you, and you can change an mLab password easy enough.

I think there is some sort of heroku reset command which might flush any lingering cached bugs.

Good luck!


Does the console log ‘listening on default port’ work?


Those are all great ideas. I’ll have some time to debug later this afternoon. Good point with the new bing key and passwords. I’ve seen a few places where they list heroku commands but have no idea where to implement them… Is there a command line in heroku?


Here’s a guess. When you push to Heroku, Heroku pulls your current master branch from your Git repository. So if your .gitignore file excludes .env from your repository, then the import of the .env module will fail. Check your Heroku logs to see if this is the culprit. It’s probably also a good idea to have .env installed as a dev dependency instead (if you don’t already) npm i -D dotenv.

You can either remove your import code from your app or do a check to make sure you don’t reference it during production, something like:

if (process.env.NODE_ENV !== 'production') { require('dotenv').config() }


.env is listed in my .gitignore. I’m still trying to figure all this out, but I was under the impression that this was the whole point of protecting your process.env variables in github…

I input the process.env variables that are referenced in the code to the config vars section (app>settings>config vars) of heroku. I was under the impression that the heroku app would look in config vars for process.env variables… Correct?

I just deleted the entire heroku app and will try to redo.


I am trying to run your app on Cloud9 but I get errors (I cloned the repo):

1 .- Using

$ node app.js

  • Error [MongoDB]:
Listening to default PORT

        process.nextTick(function() { throw err; })
MongoError: failed to connect to server [undefined:27017] on first connect
    at null.<anonymous> (/home/ubuntu/workspace/FCC_imageSearchAbstractionAPI/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:313:35)

2 .- Using

$ heroku local web
  • Error [I think is package.json]:
[WARN] No ENV file found
[WARN] ENOENT: no such file or directory, open 'Procfile'
[OKAY] package.json file found - trying 'npm start'
8:22:53 PM web.1 |  > image_search@0.0.0 start /home/ubuntu/workspace/FCC_imageSearchAbstractionAPI
8:22:53 PM web.1 |  > app.js
8:22:53 PM web.1 |  sh: 1: app.js: not found
8:22:53 PM web.1 |  npm ERR! Linux 4.2.0-c9
8:22:53 PM web.1 |  npm ERR! argv "/home/ubuntu/.nvm/versions/node/v4.5.0/bin/node" "/home/ubuntu/.nvm/versions/node/v4.5.0/bin/npm" "start"
8:22:53 PM web.1 |  npm ERR! node v4.5.0
8:22:53 PM web.1 |  npm ERR! npm  v2.15.9
8:22:53 PM web.1 |  npm ERR! file sh
8:22:53 PM web.1 |  npm ERR! code ELIFECYCLE
8:22:53 PM web.1 |  npm ERR! errno ENOENT
8:22:53 PM web.1 |  npm ERR! syscall spawn
8:22:53 PM web.1 |  npm ERR! image_search@0.0.0 start: `app.js`
8:22:53 PM web.1 |  npm ERR! spawn ENOENT
8:22:53 PM web.1 |  npm ERR! 
8:22:53 PM web.1 |  npm ERR! Failed at the image_search@0.0.0 start script 'app.js'.

Your package.json file:

“name”: “image_search”,
“version”: “0.0.0”,
“scripts”: {
“start”: “app.js” <-- here

3 .- When I changed it to node app.js:

“name”: “image_search”,
“version”: “0.0.0”,
“scripts”: {
“start”: “node app.js” <-- here

I get this error [MongoDB]:

[WARN] No ENV file found
[WARN] ENOENT: no such file or directory, open 'Procfile'
[OKAY] package.json file found - trying 'npm start'
8:41:14 PM web.1 |  > image_search@0.0.0 start /home/ubuntu/workspace/FCC_imageSearchAbstractionAPI
8:41:14 PM web.1 |  > node app.js
8:41:15 PM web.1 |  Listening to default PORT
8:41:15 PM web.1 |  /home/ubuntu/workspace/FCC_imageSearchAbstractionAPI/node_modules/mongoose/node_modules/mongodb/lib/server.js:261
8:41:15 PM web.1 |          process.nextTick(function() { throw err; })
8:41:15 PM web.1 |                                        ^
8:41:15 PM web.1 |  MongoError: failed to connect to server [undefined:27017] on first connect

The last error is MongoDB related.

Cheers and happy coding :slight_smile:


For sure, that’s why you keep the .env file out of your Git repository. What I mean is that when you require dotenv in your app, dotenv goes looking for the .env file. That file exists on your local repository (your computer) but not on the remote (GitHub). Your Heroku app comes from GitHub. When you deploy the app to Heroku, it pulls your project files and package.json (to know which modules your project is ‘dependent’ on) from your GitHub repository. If you still have the reference to dotenv in your code, Node will throw an error.

Yeah definitely. So you can put environment variables in your app like process.env.MY_VAR and Heroku will reference those variables as long as they have been defined as config variables for the project. You can do this from the command line or from your project page on Heroku (I think it’s under Settings). This is a great way to keep API keys and login info private and secure.

Try removing any code where you require dotenv to see if that will work. Double check your Heroku logs too and you should be able to see the source of the error. Keep in mind my advice is only a guess! If your only variable was adding dotenv, then that is probably the source of the error.


Thanks very much for looking into this!
This was the problem, it works now!!!
I compared the package.json file to previous ones I’ve used and the structure is different. I used express generator to build the skeleton of this app, and this is the package file it came up with…
Not sure I’ll be using the express generator in the future, it created a bunch of stuff I didn’t use and ended up deleting… Anyone else use express generator? Experiences?
Anyway, thank you everyone for your help, especially erretres for finally figuring this out and saving me even more grey hair… I really appreciate everyone taking the time to help.


jmcilhargey, thanks for the reply. I get now what you were saying. It turns out that the error was caused by something else, but this is something I’ve been confused about also, so your reply is very helpful.

So what you are saying, is that if I run npm i -D dotenv in my command line locally, I won’t have to include dotenv as a dependency?

I’m new to the back end (obviously) and the command line in general, so don’t know what i and -D mean.

I’ve been developing all my back end projects with Atom which has a powershell terminal package, so I’ve running all the command line code locally in Atom, like the git and node commands. I will try running npm i -D dotenv if that will cover the dotenv dependency from now on…

Thanks again.


Also, when you deploy to heroku, does the NODE_ENV variable automatically change from ‘development’ to ‘production’?

And if anyone wants to see the actual API, the heroku app can be found here.

[solved] Need Heroku Help ... have read other threads. Git to Heroku not working

Nice! Glad you got it sorted out!

That command npm i -D dotenv is shorthand for npm install --save-dev dotenv. For installing dependencies, normally you would use npm install --save <name-of-your-module> which so you know also be abbreviated as npm i -S <name-of-your-package>. When an installing an application for production, you would type npm install --production. That production flag tells npm not to install the modules listed under the devDependencies property in your package.json. Phew!

You can designate modules you won’t need for production as dev dependencies. Examples I know of are modules involved in unit testing, minifying files, and language conversion like CoffeeScript to Javascript or JSX (a React syntax) to Javascript. I wouldn’t worry about it until these topics come up in your learning, but good to know it’s an option!


Thanks for the explanation, thats going to help out a lot. I’ve been using npm install name-of-module -s for everything. Knowing I can designate as dev or production is great. Thanks for taking the time, much appreciated.