Matt Campbell
Matt Campbell

Reputation: 171

Cannot Deploy Node.js app on Heroku

I know this is the same question that this guy had here but I cannot comment yet as I don't have 50 reputation.

I tried following the answers that question provided but it is still having the same error with Heroku not being able to find 'express'. However, express is showing up in my dependencies in the packages.json and is all running correctly locally. I also "npm install express --save" as well as trying it with a -g flag. What else can I do so that Heroku can find 'express'?

My package.json contains:

{
  "name": "spark",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MAXXtreme",
  "license": "MIT",
  "dependencies": {
    "ejs": "^2.5.7",
    "engines": {
      "node": "6.11.2",
      "npm": "5.5.1"
    },
    "express": "^4.16.2",
    "express-fileupload": "^0.3.0",
    "express-messages": "^1.0.1",
    "express-session": "^1.15.6",
    "express-validator": "^4.3.0",
    "mongoose": "^4.12.4"
  },
  "devDependencies": {},
  "description": ""
}

My app.js contains:

var express = require('express');
var mongoose = require('mongoose');
var config = require('./config/database');

// Connect to db
mongoose.connect(config.database);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
  console.log('Connected to MongoDB');
});

// Init app
var app = express();

// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// Set public folder
app.use(express.static(path.join(__dirname, 'public')));

// Start the server
var port = process.env.PORT || 3000;
app.listen(port, function () {
    console.log('Server started on port ' + port);
});

Here are the logs from Heroku CLI:

2017-10-30T13:22:23.512366+00:00 app[api]: Initial release by user [email protected]
2017-10-30T13:22:23.512366+00:00 app[api]: Release v1 created by user [email protected]
2017-10-30T13:22:23.607220+00:00 app[api]: Release v2 created by user [email protected]
2017-10-30T13:22:23.607220+00:00 app[api]: Enable Logplex by user [email protected]
2017-10-30T13:28:32.000000+00:00 app[api]: Build started by user [email protected]
2017-10-30T13:29:04.387219+00:00 app[api]: Release v3 created by user [email protected]
2017-10-30T13:28:32.000000+00:00 app[api]: Build succeeded
2017-10-30T13:29:04.401658+00:00 app[api]: Scaled to web@1:Free by user [email protected]
2017-10-30T13:29:04.387219+00:00 app[api]: Deploy dc2a122a by user [email protected]
2017-10-30T13:29:06.586186+00:00 heroku[web.1]: Starting process with command `node --debug=5858 app.js`
2017-10-30T13:29:08.458266+00:00 app[web.1]: Debugger listening on [::]:5858
2017-10-30T13:29:08.521127+00:00 app[web.1]: module.js:471
2017-10-30T13:29:08.521129+00:00 app[web.1]:     throw err;
2017-10-30T13:29:08.521130+00:00 app[web.1]:     ^
2017-10-30T13:29:08.521131+00:00 app[web.1]:
2017-10-30T13:29:08.521131+00:00 app[web.1]: Error: Cannot find module 'express'
2017-10-30T13:29:08.521132+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:469:15)
2017-10-30T13:29:08.521133+00:00 app[web.1]:     at Function.Module._load (module.js:417:25)
2017-10-30T13:29:08.521134+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-10-30T13:29:08.521134+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-10-30T13:29:08.521135+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:1:77)
2017-10-30T13:29:08.521135+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-10-30T13:29:08.521136+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-10-30T13:29:08.521136+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-10-30T13:29:08.521137+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-10-30T13:29:08.521138+00:00 app[web.1]:     at Function.Module._load (module.js:438:3)
2017-10-30T13:29:08.617580+00:00 heroku[web.1]: State changed from starting to crashed
2017-10-30T13:29:08.620539+00:00 heroku[web.1]: State changed from crashed to starting
2017-10-30T13:29:08.598008+00:00 heroku[web.1]: Process exited with status 1

Upvotes: 0

Views: 856

Answers (1)

Syed
Syed

Reputation: 269

Try Placing engines outside the dependencies in your package.json

{
  "name": "spark",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MAXXtreme",
  "license": "MIT",
  "engines": {
    "node": "6.11.2",
    "npm": "5.5.1"
  },
  "dependencies": {
    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "express-fileupload": "^0.3.0",
    "express-messages": "^1.0.1",
    "express-session": "^1.15.6",
    "express-validator": "^4.3.0",
    "mongoose": "^4.12.4"
  },
  "devDependencies": {},
  "description": ""
}

Upvotes: 1

Related Questions