Stopping Heroku from running npm start + what to run instead? - node.js

Disclaimer: I'm a node.js/grunt/bower newbie.
I have a node.js/grunt/bower application that I'm trying to deploy on Heroku. Heroku builds the application as expected, but then it tries to run "npm start" which I did not specify in the package.json file. This command fails, I'm guessing because it is not a server application, and I can not see the actual application deployed in it's place on Heroku. My application can be found here: https://github.com/uzilan/sufusku
So - how do I get Heroku not to run the "npm start" command, and what should it run instead? After running grunt build, the application is available on the dist folder.
package.json:
{
"name": "sufusku",
"version": "0.0.1",
"description": "Sufusku - makes your annoying sudoku easier",
"repository": {
"type": "git",
"url": "git://github.com/uzilan/sufusku.git"
},
"license": "(MIT OR Apache-2.0)",
"dependencies": {
"bower": "^1.4.1",
"grunt": "0.4.5",
"grunt-cli": "0.1.13",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-concat": "^0.5.1",
"grunt-contrib-connect": "^0.11.2",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-cssmin": "^0.13.0",
"grunt-contrib-htmlmin": "^0.4.0",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-ng-annotate": "^1.0.1",
"load-grunt-config": "^0.17.2",
"time-grunt": "^1.2.1"
},
"devDependencies": {},
"engines": {
"node": ">=0.8.0"
},
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install && grunt build"
}
}
Gruntfile.js:
module.exports = function (grunt) {
var appname = require('./package.json').name;
require('load-grunt-config')(grunt, {
data: {
dist: 'dist/' + appname,
app: 'src',
distscripts: 'dist/' + appname + '/scripts',
diststyles: 'dist/' + appname + '/styles',
disthtml: 'dist/' + appname,
disttest: 'dist/' + appname + '/test',
appstyles: 'src/css'
}
});
require('time-grunt')(grunt);
grunt.registerTask('build', ['clean:dist', 'ngAnnotate:application', 'uglify', 'htmlmin', 'cssmin', 'copy:serve', 'concat:components']);
grunt.registerTask('serve', ['build', 'connect:livereload', 'watch']);
grunt.registerTask('default', function () {
grunt.task.run(['build']);
});
};
Heroku log:
...
heroku[web.1]: Starting process with command `npm start`
app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
app[web.1]: npm ERR! Linux 3.13.0-57-generic
app[web.1]: npm ERR! npm v2.11.3
app[web.1]: npm ERR! missing script: start
app[web.1]: npm ERR! Please include the following file with any support request:
app[web.1]: npm ERR!
app[web.1]: npm ERR! node v0.12.7
app[web.1]:
app[web.1]: npm ERR! /app/npm-debug.log
app[web.1]: npm ERR! If you need help, you may report this error at:
app[web.1]:
app[web.1]: npm ERR! <https://github.com/npm/npm/issues>
heroku[web.1]: Process exited with status 1
heroku[web.1]: State changed from starting to crashed
heroku[web.1]: State changed from crashed to starting
heroku[web.1]: Starting process with command `npm start`
app[web.1]: npm ERR! Linux 3.13.0-57-generic
app[web.1]: npm ERR! node v0.12.7
app[web.1]:
app[web.1]: npm ERR! missing script: start
...

npm start is the default web process run when no other process has been specified in a Procfile. Since you say you don't want a server (a web process), the first thing to do is to scale the default process to zero:
heroku scale web=0
Next, you'll want to tell Heroku what process you want to run instead of web by adding its process types to a file called Procfile. For instance, if your app starts with node foobar.js, you could create a Procfile that looks like:
bot: node foobar.js
Then scale the 'bot' process up to at least one:
heroku scale bot=1
Looking at your code above, even though you say it isn't a server-based application, it looks very much like a server-based application. How do you start your app locally? Whatever the answer that should probably go into the Procfile, which you can learn more about here:
https://devcenter.heroku.com/articles/procfile
disclosure: I'm the Node.js platform owner at Heroku

Thanks for the answer. After trying the tutorial mentioned above and looking at the solutions in node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON] I ended up writing a server.js file which solved the problem. And it looks like this:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get(/^(.+)$/, function (req, res) {
res.sendFile(__dirname + req.params[0]);
});
var PORT = process.env.PORT || 3000;
app.listen(PORT);

Related

Babel-node not found - Heroku Deploy

I am trying to deploy a simple Ecommerce website that has Reactjs , Node, Express and MongoDB. I have followed a tutorial to deploy it to the heroku. I have used babel but in the tutorial, it wasn't used. I know we don't use babel for production and used only for the development purpose, but I am not sure how I would still deploy my website.
The server is working perfectly locally and also the Heroku build is successful. But when I visit my deployed app on heroku it says application error.
Here are the logs from Heroku:
2020-06-29T16:04:44.812179+00:00 heroku[web.1]: Starting process with command `npm start`
2020-06-29T16:04:48.072669+00:00 app[web.1]:
2020-06-29T16:04:48.072690+00:00 app[web.1]: > e-commerce-site#1.0.0 start /app
2020-06-29T16:04:48.072691+00:00 app[web.1]: > babel-node backend/server.js
2020-06-29T16:04:48.072691+00:00 app[web.1]:
2020-06-29T16:04:48.105827+00:00 app[web.1]: sh: 1: babel-node: not found
2020-06-29T16:04:48.110266+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-29T16:04:48.111030+00:00 app[web.1]: npm ERR! syscall spawn
2020-06-29T16:04:48.111915+00:00 app[web.1]: npm ERR! file sh
2020-06-29T16:04:48.112482+00:00 app[web.1]: npm ERR! errno ENOENT
2020-06-29T16:04:48.114018+00:00 app[web.1]: npm ERR! e-commerce-site#1.0.0 start: `babel-node backend/server.js`
2020-06-29T16:04:48.114155+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-06-29T16:04:48.114342+00:00 app[web.1]: npm ERR!
2020-06-29T16:04:48.114508+00:00 app[web.1]: npm ERR! Failed at the e-commerce-site#1.0.0 start script.
2020-06-29T16:04:48.114601+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-06-29T16:04:48.133545+00:00 app[web.1]:
2020-06-29T16:04:48.134148+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-06-29T16:04:48.134771+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-06-
29T16_04_48_124Z-debug.log
2020-06-29T16:04:48.213867+00:00 heroku[web.1]: Process exited with status 1
2020-06-29T16:04:48.265610+00:00 heroku[web.1]: State changed from starting to crashed
2020-06-29T16:04:48.268292+00:00 heroku[web.1]: State changed from crashed to starting
2020-06-29T16:04:57.328678+00:00 heroku[web.1]: Starting process with command `npm start`
2020-06-29T16:05:00.299582+00:00 app[web.1]:
2020-06-29T16:05:00.299595+00:00 app[web.1]: > e-commerce-site#1.0.0 start /app
2020-06-29T16:05:00.299595+00:00 app[web.1]: > babel-node backend/server.js
2020-06-29T16:05:00.299596+00:00 app[web.1]:
2020-06-29T16:05:00.308802+00:00 app[web.1]: sh: 1: babel-node: not found
2020-06-29T16:05:00.312668+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-29T16:05:00.313206+00:00 app[web.1]: npm ERR! syscall spawn
2020-06-29T16:05:00.313637+00:00 app[web.1]: npm ERR! file sh
2020-06-29T16:05:00.314093+00:00 app[web.1]: npm ERR! errno ENOENT
2020-06-29T16:05:00.315953+00:00 app[web.1]: npm ERR! e-commerce-site#1.0.0 start: `babel-node backend/server.js`
2020-06-29T16:05:00.316283+00:00 app[web.1]: npm ERR! spawn ENOENT
2020-06-29T16:05:00.316613+00:00 app[web.1]: npm ERR!
2020-06-29T16:05:00.316974+00:00 app[web.1]: npm ERR! Failed at the e-commerce-site#1.0.0 start script.
2020-06-29T16:05:00.317312+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-06-29T16:05:00.344371+00:00 app[web.1]:
2020-06-29T16:05:00.344828+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-06-29T16:05:00.345202+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-06-29T16_05_00_319Z-debug.log
2020-06-29T16:05:00.406294+00:00 heroku[web.1]: Process exited with status 1
2020-06-29T16:05:00.466244+00:00 heroku[web.1]: State changed from starting to crashed
File Name: Server.js
The file where I have all the connections for MongoDB:
import express from "express";
import data from "./data";
import dotenv from "dotenv";
import config from "./config";
import mongoose from "mongoose";
import userRoute from "./routes/userRoute";
import productRoute from "./routes/productRoute";
import bodyParser from "body-parser";
dotenv.config();
const mongodbUrl = config.MONGODB_URL;
const PORT = process.env.PORT || 5000;
mongoose.connect(mongodbUrl , {
useNewUrlParser : true,
useUnifiedTopology: true,
useCreateIndex: true
}).catch(error => console.log(error.reason));
const app = express();
app.use(bodyParser.json());
app.use("/api/users" , userRoute);
app.use("/api/products" , productRoute);
if (process.env.NODE_ENV === "production"){
app.use(express.static("./frontend/build"));
}
app.listen(PORT , console.log(`Server started at ${PORT}`));
File Name: config.js
export default{
MONGODB_URL: process.env.MONGODB_URL || "mongodb://localhost/ecommercesite",
JWT_SECRET: process.env.JWT_SECRET || "somethingsecret"
}
File Name: Package.json
{
"name": "e-commerce-site",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build" : "cd frontend && npm run build",
"install-client" : "cd frontend && npm install",
"heroku-postbuild" : "npm run install-client && npm run build",
"start": "babel-node backend/server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.16"
},
"devDependencies": {
"#babel/cli": "^7.10.1",
"#babel/core": "^7.10.1",
"#babel/node": "^7.10.1",
"#babel/preset-env": "^7.10.1",
"nodemon": "^2.0.4"
}
}

'npm start' in node.js doesn't works

node App.js works, but npm start doesn't works.
I just followed this nodejs tutorial (tutorial is written by Korean).
I tried to change code to:
"scripts": {
"start": "webpack-dev-server"
}
but it didn't work.
this is my code:
App.js
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
package.json
{
"name": "codlab-nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node js"
},
"author": "",
"license": "ISC"
}
package-lock.json
{
"name": "codlab-nodejs",
"version": "1.0.0",
"lockfileVersion": 1
}
This is the err msg.
C:\Users\김동희\codlab-nodejs>npm start
> codlab-nodejs#1.0.0 start C:\Users\김동희\codlab-nodejs
> node js
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'C:\Users\김동희\codlab-nodejs\js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! codlab-nodejs#1.0.0 start: `node js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the codlab-nodejs#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\김동희\AppData\Roaming\npm-cache\_logs\2019-08-03T14_03_42_211Z-debug.log
In your package.json change the value of the property "start".
Replace:
"start": "node js"
With:
"start": "node App.js"
The start npm script is trying to run a file called js, which does not exist and consequently fails.
Instead, set your start npm script to be the correct command that asks Node.js to run your App.js file.
Your package.json would include something like this:
"scripts": {
"start": "node App.js"
}

Parse Server not recognizing APP_ID

I have installed Parse Server on Heroku and forked the Parse Server Example. When I git push heroku master I get the error
"You must provide an appIp!"
Error log:
019-03-20T11:03:56.517489+00:00 heroku[web.1]: Starting process with command `npm start`
2019-03-20T11:03:59.418738+00:00 app[web.1]:
2019-03-20T11:03:59.418754+00:00 app[web.1]: > parse-server-example#1.4.0 start /app
2019-03-20T11:03:59.418756+00:00 app[web.1]: > node index.js
2019-03-20T11:03:59.418757+00:00 app[web.1]:
2019-03-20T11:04:01.887096+00:00 heroku[web.1]: State changed from starting to crashed
2019-03-20T11:04:01.724008+00:00 app[web.1]:
2019-03-20T11:04:01.724030+00:00 app[web.1]: /app/node_modules/parse-server/lib/ParseServer.js:218
2019-03-20T11:04:01.724032+00:00 app[web.1]: throw err;
2019-03-20T11:04:01.724033+00:00 app[web.1]: ^
2019-03-20T11:04:01.724105+00:00 app[web.1]: You must provide an appId!
2019-03-20T11:04:01.779713+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-03-20T11:04:01.780520+00:00 app[web.1]: npm ERR! errno 7
2019-03-20T11:04:01.782955+00:00 app[web.1]: npm ERR! parse-server-example#1.4.0 start: `node index.js`
2019-03-20T11:04:01.783173+00:00 app[web.1]: npm ERR! Exit status 7
2019-03-20T11:04:01.783716+00:00 app[web.1]: npm ERR!
2019-03-20T11:04:01.783983+00:00 app[web.1]: npm ERR! Failed at the parse-server-example#1.4.0 start script.
2019-03-20T11:04:01.784194+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-03-20T11:04:01.804701+00:00 app[web.1]:
2019-03-20T11:04:01.805007+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-03-20T11:04:01.805196+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2019-03-20T11_04_01_786Z-debug.log
2019-03-20T11:04:01.866235+00:00 heroku[web.1]: Process exited with status 7
index.js:
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://key
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || 'myMasterKey',
serverURL: process.env.SERVER_URL || 'http://myApp.herokuapp.com/parse',
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
}
});
var server = ParseServer({
verifyUserEmails: true,
publicServerURL: 'myApp.herokuapp.com/parse',
appName: 'myAppName',
emailAdapter: {
module: 'parse-server-simple-mailgun-adapter',
options: {
fromAddress: 'parse#example.com',
domain: 'domainFromMailGun.mailgun.org',
apiKey: 'myAPIKey',
}
}
});
I have provided the appId in appId: process.env.APP_ID || 'myAppId', or am I missing something here? Does the appId have to provided elsewhere?
First of all, please invalidate those MongoDB credentials immediately. They are forever compromised, and you need to generate new ones. Editing them out of your question is not enough.
I'm not totally clear on what you're trying to do here, but you're actually instantiating two Parse servers. You do provide an appId for the first one (api), but not for the second (server).
You probably only need one Parse Server. If you do actually need two for some reason, each one will need an appId.

My node + express app deploys fine locally but will not deploy when pushed to heroku

first off my apologies if this is a basic solve. I'm new to Node + Express and still trying to grapple a few concepts!
I have a Node + Express app that starts locally but when I push to heroku or run heroku local web it exits with code 0 and produces no error code for me to diagnose.
Here is the specific output from the logs when I deploy to Heroku:
2018-06-30T11:58:37.052754+00:00 heroku[web.1]: Starting process with command `npm run build`
2018-06-30T11:58:39.336703+00:00 app[web.1]:
2018-06-30T11:58:39.336728+00:00 app[web.1]: > bookworm-api#1.0.0 build /app
2018-06-30T11:58:39.336733+00:00 app[web.1]: > npm run clean && npm run build-babel
2018-06-30T11:58:39.336734+00:00 app[web.1]:
2018-06-30T11:58:39.777160+00:00 app[web.1]:
2018-06-30T11:58:39.777199+00:00 app[web.1]: > bookworm-api#1.0.0 clean /app
2018-06-30T11:58:39.777201+00:00 app[web.1]: > rm -rf build && mkdir build
2018-06-30T11:58:39.777202+00:00 app[web.1]:
2018-06-30T11:58:40.305583+00:00 app[web.1]:
2018-06-30T11:58:40.305603+00:00 app[web.1]: > bookworm-api#1.0.0 build-babel /app
2018-06-30T11:58:40.305605+00:00 app[web.1]: > babel -d ./build ./src -s
2018-06-30T11:58:40.305610+00:00 app[web.1]:
2018-06-30T11:58:41.146122+00:00 app[web.1]: src/index.js -> build/index.js
2018-06-30T11:58:41.196474+00:00 app[web.1]: src/mailer.js -> build/mailer.js
2018-06-30T11:58:41.242714+00:00 app[web.1]: src/models/Circuit.js -> build/models/Circuit.js
2018-06-30T11:58:41.293304+00:00 app[web.1]: src/models/User.js -> build/models/User.js
2018-06-30T11:58:41.346985+00:00 app[web.1]: src/routes/auth.js -> build/routes/auth.js
2018-06-30T11:58:41.362022+00:00 app[web.1]: src/routes/circuits.js -> build/routes/circuits.js
2018-06-30T11:58:41.380720+00:00 app[web.1]: src/routes/stripe.js -> build/routes/stripe.js
2018-06-30T11:58:41.411474+00:00 app[web.1]: src/routes/users.js -> build/routes/users.js
2018-06-30T11:58:41.423692+00:00 app[web.1]: src/utils/parseErrors.js -> build/utils/parseErrors.js
2018-06-30T11:58:41.508066+00:00 heroku[web.1]: Process exited with status 0
2018-06-30T11:58:41.532442+00:00 heroku[web.1]: State changed from starting to crashed
Here is the output when I run Heroku local web
[OKAY] Loaded ENV .env File as KEY=VALUE Format
8:04:38 AM web.1 | > bookworm-api#1.0.0 build /Users/kylependergast/Desktop/projects/bookworm/bookworm-api
8:04:38 AM web.1 | > npm run clean && npm run build-babel
8:04:38 AM web.1 | > bookworm-api#1.0.0 clean /Users/kylependergast/Desktop/projects/bookworm/bookworm-api
8:04:38 AM web.1 | > rm -rf build && mkdir build
8:04:39 AM web.1 | > bookworm-api#1.0.0 build-babel /Users/kylependergast/Desktop/projects/bookworm/bookworm-api
8:04:39 AM web.1 | > babel -d ./build ./src -s
8:04:39 AM web.1 | src/index.js -> build/index.js
8:04:39 AM web.1 | src/mailer.js -> build/mailer.js
8:04:39 AM web.1 | src/models/Circuit.js -> build/models/Circuit.js
8:04:39 AM web.1 | src/models/User.js -> build/models/User.js
8:04:39 AM web.1 | src/routes/auth.js -> build/routes/auth.js
8:04:39 AM web.1 | src/routes/circuits.js -> build/routes/circuits.js
8:04:39 AM web.1 | src/routes/stripe.js -> build/routes/stripe.js
8:04:39 AM web.1 | src/routes/users.js -> build/routes/users.js
8:04:39 AM web.1 | src/utils/parseErrors.js -> build/utils/parseErrors.js
8:04:39 AM web.1 Exited Successfully
Here is my package.json file:
{
"name": "bookworm-api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "nodemon --exec babel-node -- src/index.js",
"clean": "rm -rf build && mkdir build",
"build-babel": "babel -d ./build ./src -s",
"build": "npm run clean && npm run build-babel",
"start": "npm run build && node ./build/index.js"
},
"engines": {
"node": "9.4.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.3",
"babel-preset-env": "^1.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-prettier": "^2.6.0",
"nodemon": "^1.17.4"
},
"dependencies": {
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"bcrypt": "^2.0.1",
"bluebird": "^3.5.1",
"body-parser": "^1.18.2",
"dotenv": "^5.0.1",
"express": "^4.16.3",
"jsonwebtoken": "^8.2.1",
"lodash": "^4.17.10",
"mongodb": "^3.1.0",
"mongoose": "^4.11.10",
"mongoose-unique-validator": "^2.0.1",
"nodemailer": "^4.6.4",
"request-promise": "^4.2.2",
"stripe": "^6.1.1",
"uuid": "^3.2.1",
"xml2js": "^0.4.19"
}
}
And here is my index.js file:
import express from 'express';
import path from 'path';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
import Promise from 'bluebird';
import auth from './routes/auth';
import users from './routes/users';
import circuits from "./routes/circuits";
import stripe from './routes/stripe';
dotenv.config();
const app = express();
app.use(bodyParser.json());
mongoose.Promise = Promise;
mongoose.connect(
process.env.MONGODB_URI || process.env.MONGODB_URL_DEV_LOCAL,
{useMongoClient: true});
app.use('/api/auth', auth);
app.use('/api/users', users);
app.use('/api/circuits', circuits);
app.use('/api/stripe', stripe);
app.post('/api/auth', (req, res) => {
res.status(400).json({ errors: {global: "Invalid Credentials" } });
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
})
var port = process.env.PORT || 8080
app.listen(port || 8080, function(){
console.log("App is running on port " + port);
});
Here is my Procfile
web: npm run build
I'm sure someone else has run into this issue but I've been unable to find resources that suggest what next steps to take. If you can point me to a relevant resource or provide the answer yourself it would be very much appreciated!
Your Procfile seems to be defined such that you're building stuff when starting the process. The build succeeds (returns 0), but then the process stops, which Heroku thinks is a bad condition, cause web servers shouldn't stop.
You need to actually run the web server in Heroku. Something like web: npm run start.
Though I would separate the build & run stages. You should do the build locally, or on a build machine (or a build service, such as Travis, Circle etc - they have Heroku integrations as well), and then push something compiled to Heroku. So Procfile could only be web: node ./build/index.js or something like that.

Hosting an express app on Heroku error with mongoose

I'm building a mean stack app with ionic 2 and I want to host it on Heroku. The app is finished and runs fine locally and I have it connected to a mLabs database. When I push the app to Heroku I am getting the following error in the logs.
2017-04-10T12:32:15.558949+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2017-04-10T12:32:15.558953+00:00 app[web.1]: at Module._compile (module.js:570:32)
2017-04-10T12:32:15.558954+00:00 app[web.1]: at Object.Module._extensions..js (module.js:579:10)
2017-04-10T12:32:15.558951+00:00 app[web.1]: at Function.Module._load (module.js:417:25)
2017-04-10T12:32:15.558952+00:00 app[web.1]: at require (internal/module.js:20:19)
2017-04-10T12:32:15.558952+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:4:16)
2017-04-10T12:32:15.558951+00:00 app[web.1]: at Module.require (module.js:497:17)
2017-04-10T12:32:15.558954+00:00 app[web.1]: at Module.load (module.js:487:32)
2017-04-10T12:32:15.558955+00:00 app[web.1]: at Function.Module._load (module.js:438:3)
2017-04-10T12:32:15.558955+00:00 app[web.1]: at tryModuleLoad (module.js:446:12)
2017-04-10T12:32:15.558950+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:469:15)
2017-04-10T12:32:15.567486+00:00 app[web.1]:
2017-04-10T12:32:15.577504+00:00 app[web.1]: npm ERR! Linux 3.13.0-112-generic
2017-04-10T12:32:15.577867+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2017-04-10T12:32:15.578113+00:00 app[web.1]: npm ERR! node v6.10.2
2017-04-10T12:32:15.578341+00:00 app[web.1]: npm ERR! npm v3.10.10
2017-04-10T12:32:15.578578+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-04-10T12:32:15.578785+00:00 app[web.1]: npm ERR! ionic-hello-world# start: `node server.js`
2017-04-10T12:32:15.578954+00:00 app[web.1]: npm ERR! Exit status 1
2017-04-10T12:32:15.579249+00:00 app[web.1]: npm ERR!
2017-04-10T12:32:15.579426+00:00 app[web.1]: npm ERR! Failed at the ionic-hello-world# start script 'node server.js'.
2017-04-10T12:32:15.579621+00:00 app[web.1]: npm ERR! Make sure you have the latest version of node.js and npm installed.
2017-04-10T12:32:15.579920+00:00 app[web.1]: npm ERR! not with npm itself.
2017-04-10T12:32:15.579746+00:00 app[web.1]: npm ERR! If you do, this is most likely a problem with the ionic-hello-world package,
2017-04-10T12:32:15.580101+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-04-10T12:32:15.580265+00:00 app[web.1]: npm ERR! node server.js
2017-04-10T12:32:15.580757+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-04-10T12:32:15.580428+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-04-10T12:32:15.580601+00:00 app[web.1]: npm ERR! npm bugs ionic-hello-world
2017-04-10T12:32:15.580878+00:00 app[web.1]: npm ERR! npm owner ls ionic-hello-world
2017-04-10T12:32:15.580993+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-04-10T12:32:15.588769+00:00 app[web.1]:
2017-04-10T12:32:15.589345+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-04-10T12:32:15.589646+00:00 app[web.1]: npm ERR! /app/npm-debug.log
2017-04-10T12:32:15.679397+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-10T12:32:15.680194+00:00 heroku[web.1]: State changed from crashed to starting
2017-04-10T12:32:15.673049+00:00 heroku[web.1]: Process exited with status 1
2017-04-10T12:32:18.568796+00:00 heroku[web.1]: Starting process with command `npm start`
2017-04-10T12:32:21.743026+00:00 app[web.1]:
2017-04-10T12:32:21.743041+00:00 app[web.1]: > ionic-hello-world# start /app
2017-04-10T12:32:21.743042+00:00 app[web.1]: > node server.js
2017-04-10T12:32:21.743042+00:00 app[web.1]:
2017-04-10T12:32:22.058223+00:00 app[web.1]: module.js:471
2017-04-10T12:32:22.058237+00:00 app[web.1]: ^
2017-04-10T12:32:22.058236+00:00 app[web.1]: throw err;
2017-04-10T12:32:22.058239+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2017-04-10T12:32:22.058238+00:00 app[web.1]:
2017-04-10T12:32:22.058241+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:469:15)
2017-04-10T12:32:22.058242+00:00 app[web.1]: at Function.Module._load (module.js:417:25)
2017-04-10T12:32:22.058242+00:00 app[web.1]: at Module.require (module.js:497:17)
2017-04-10T12:32:22.058243+00:00 app[web.1]: at require (internal/module.js:20:19)
2017-04-10T12:32:22.058243+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:4:16)
2017-04-10T12:32:22.058244+00:00 app[web.1]: at Module._compile (module.js:570:32)
2017-04-10T12:32:22.058245+00:00 app[web.1]: at Object.Module._extensions..js (module.js:579:10)
2017-04-10T12:32:22.058245+00:00 app[web.1]: at Module.load (module.js:487:32)
2017-04-10T12:32:22.058246+00:00 app[web.1]: at tryModuleLoad (module.js:446:12)
2017-04-10T12:32:22.058246+00:00 app[web.1]: at Function.Module._load (module.js:438:3)
2017-04-10T12:32:22.067921+00:00 app[web.1]:
2017-04-10T12:32:22.080492+00:00 app[web.1]: npm ERR! Linux 3.13.0-112-generic
2017-04-10T12:32:22.080827+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2017-04-10T12:32:22.080991+00:00 app[web.1]: npm ERR! node v6.10.2
2017-04-10T12:32:22.081154+00:00 app[web.1]: npm ERR! npm v3.10.10
2017-04-10T12:32:22.081323+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-04-10T12:32:22.081657+00:00 app[web.1]: npm ERR! Exit status 1
2017-04-10T12:32:22.081825+00:00 app[web.1]: npm ERR!
2017-04-10T12:32:22.081993+00:00 app[web.1]: npm ERR! Failed at the ionic-hello-world# start script 'node server.js'.
2017-04-10T12:32:22.082162+00:00 app[web.1]: npm ERR! Make sure you have the latest version of node.js and npm installed.
2017-04-10T12:32:22.081490+00:00 app[web.1]: npm ERR! ionic-hello-world# start: `node server.js`
2017-04-10T12:32:22.082332+00:00 app[web.1]: npm ERR! If you do, this is most likely a problem with the ionic-hello-world package,
2017-04-10T12:32:22.082335+00:00 app[web.1]: npm ERR! not with npm itself.
2017-04-10T12:32:22.082498+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-04-10T12:32:22.082666+00:00 app[web.1]: npm ERR! node server.js
2017-04-10T12:32:22.082832+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-04-10T12:32:22.083001+00:00 app[web.1]: npm ERR! npm bugs ionic-hello-world
2017-04-10T12:32:22.083168+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-04-10T12:32:22.083170+00:00 app[web.1]: npm ERR! npm owner ls ionic-hello-world
2017-04-10T12:32:22.083338+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-04-10T12:32:22.092234+00:00 app[web.1]:
2017-04-10T12:32:22.092236+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-04-10T12:32:22.092237+00:00 app[web.1]: npm ERR! /app/npm-debug.log
2017-04-10T12:32:22.188511+00:00 heroku[web.1]: Process exited with status 1
2017-04-10T12:32:22.201942+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-10T12:32:50.415338+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=dry-retreat-69141.herokuapp.com request_id=898a0974-f3eb-47b2-a7ca-77a37cf99131 fwd="193.1.57.4" dyno= connect= service= status=503 bytes= protocol=https
2017-04-10T12:35:00.000000+00:00 app[api]: Build started by user alanmurphy1995#gmail.com
2017-04-10T12:36:27.620672+00:00 app[api]: Release v6 created by user alanmurphy1995#gmail.com
2017-04-10T12:36:27.620672+00:00 app[api]: Deploy 9f02cb4 by user alanmurphy1995#gmail.com
2017-04-10T12:36:27.804049+00:00 heroku[web.1]: State changed from crashed to starting
2017-04-10T12:36:27.782474+00:00 app[api]: Release v6 created by user alanmurphy1995#gmail.com
2017-04-10T12:35:00.000000+00:00 app[api]: Build succeeded
2017-04-10T12:36:33.381932+00:00 heroku[web.1]: Starting process with command `npm start`
2017-04-10T12:36:36.544686+00:00 app[web.1]:
2017-04-10T12:36:36.544709+00:00 app[web.1]: > ionic-hello-world# start /app
2017-04-10T12:36:36.544710+00:00 app[web.1]: > node server.js
2017-04-10T12:36:36.544711+00:00 app[web.1]:
2017-04-10T12:36:37.082619+00:00 app[web.1]: App listening on port 8080
2017-04-10T12:37:33.784488+00:00 app[web.1]: Error waiting for process to terminate: No child processes
2017-04-10T12:37:33.760270+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-04-10T12:37:33.760270+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-04-10T12:37:33.878656+00:00 heroku[web.1]: Process exited with status 22
2017-04-10T12:37:33.889982+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-10T12:37:34.773926+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=dry-retreat-69141.herokuapp.com request_id=6ba08c4e-a269-4530-b0db-192da56e2724 fwd="193.1.57.4" dyno= connect= service= status=503 bytes= protocol=https
For reference this is my server.js file
// Set up
var express = require('express');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)
var cors = require('cors');
// Configuration
//mongoose.connect('mongodb://localhost/reviewking');
mongoose.connect('Connecting to mLabs here URI not included for security reasons');
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
app.use(cors());
app.use(express.static("www")); // Our Ionic app build is in the www folder (kept up-to-date by the Ionic CLI using 'ionic serve')
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Models
var Review = mongoose.model('Review', {
title: String,
description: String,
rating: Number
});
// Routes
// Get reviews
app.get('/api/reviews', function(req, res) {
console.log("fetching reviews");
// use mongoose to get all reviews in the database
Review.find(function(err, reviews) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(reviews); // return all reviews in JSON format
});
});
// create review and send back all reviews after creation
app.post('/api/reviews', function(req, res) {
console.log("creating review");
// create a review, information comes from request from Ionic
Review.create({
title : req.body.title,
description : req.body.description,
rating: req.body.rating,
done : false
}, function(err, review) {
if (err)
res.send(err);
// get and return all the reviews after you create another
Review.find(function(err, reviews) {
if (err)
res.send(err)
res.json(reviews);
});
});
});
// delete a review
app.delete('/api/reviews/:review_id', function(req, res) {
Review.remove({
_id : req.params.review_id
}, function(err, review) {
});
});
// listen (start app with node server.js) ======================================
app.listen(8080);
console.log("App listening on port 8080");
And this is my package.json file.
{
"name": "ionic-hello-world",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"private": true,
"scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve",
"start": "node server.js"
},
"dependencies": {
"#angular/common": "2.2.1",
"#angular/compiler": "2.2.1",
"#angular/compiler-cli": "2.2.1",
"#angular/core": "2.2.1",
"#angular/forms": "2.2.1",
"#angular/http": "2.2.1",
"#angular/platform-browser": "2.2.1",
"#angular/platform-browser-dynamic": "2.2.1",
"#angular/platform-server": "2.2.1",
"#ionic/storage": "1.1.7",
"#types/cordova-plugin-network-information": "0.0.3",
"angularfire2": "^2.0.0-beta.6",
"firebase": "^3.3.0",
"ionic-angular": "2.1.0",
"ionic-native": "2.4.1",
"ionicons": "3.0.0",
"rxjs": "5.0.0-beta.12",
"sw-toolbox": "3.4.0",
"zone.js": "0.6.26",
"#ionic/app-scripts": "1.1.3",
"typescript": "2.0.9",
"mongoose": "^4.6.2",
"body-parser": "^1.15.2",
"cors": "^2.8.0",
"del": "2.2.0",
"express": "^4.14.0",
"http": "0.0.0",
"method-override": "^2.3.6",
"morgan": "^1.7.0",
"superlogin": "^0.6.1"
},
"devDependencies": {
"#ionic/app-scripts": "1.1.3",
"typescript": "2.0.9",
"mongoose": "^4.6.2",
"body-parser": "^1.15.2",
"cors": "^2.8.0",
"del": "2.2.0",
"express": "^4.14.0",
"http": "0.0.0",
"method-override": "^2.3.6",
"morgan": "^1.7.0",
"superlogin": "^0.6.1"
},
"cordovaPlugins": [
"cordova-plugin-whitelist",
"cordova-plugin-console",
"cordova-plugin-statusbar",
"cordova-plugin-device",
"cordova-plugin-splashscreen",
"ionic-plugin-keyboard"
],
"cordovaPlatforms": [],
"description": "ReviewApp: An Ionic project"
}
Any help with this error or a solution around this so I can push a working app to Heroku would help greatly.
Removing mongoose from devDependencies should fix it.
Explanation: mongoose is listed twice in package.json - once in dependencies and once in devDependencies, which don't get installed by Heroku.
I also suggest you alphabetize those dependency lists so that this kind of issue is easier to catch in the future.

Resources