I am having issues deploying to Heroku. I have uploaded the relevant env variables under "Config Vars" within Heroku Dashboard but am wondering why I am still getting hit with the following error below:
2022-07-22T06:47:59.068313+00:00 heroku[web.1]: Starting process with command `npm start`
2022-07-22T06:48:00.732711+00:00 app[web.1]:
2022-07-22T06:48:00.732804+00:00 app[web.1]: > project-management-webapp#1.0.0 start
2022-07-22T06:48:00.732804+00:00 app[web.1]: > npm install && cd server && node index.js
2022-07-22T06:48:00.732805+00:00 app[web.1]:
2022-07-22T06:48:01.693914+00:00 app[web.1]:
2022-07-22T06:48:01.693932+00:00 app[web.1]: up to date, audited 128 packages in 601ms
2022-07-22T06:48:01.694072+00:00 app[web.1]:
2022-07-22T06:48:01.694106+00:00 app[web.1]: 14 packages are looking for funding
2022-07-22T06:48:01.694126+00:00 app[web.1]: run `npm fund` for details
2022-07-22T06:48:01.695406+00:00 app[web.1]:
2022-07-22T06:48:01.695406+00:00 app[web.1]: found 0 vulnerabilities
2022-07-22T06:48:02.296149+00:00 heroku[web.1]: State changed from starting to crashed
2022-07-22T06:48:02.083051+00:00 app[web.1]: Server running on port 24830
2022-07-22T06:48:02.087994+00:00 app[web.1]: /app/node_modules/mongodb-connection-string-url/lib/index.js:86
2022-07-22T06:48:02.088006+00:00 app[web.1]: throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');
2022-07-22T06:48:02.088007+00:00 app[web.1]: ^
2022-07-22T06:48:02.088007+00:00 app[web.1]:
2022-07-22T06:48:02.088013+00:00 app[web.1]: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
2022-07-22T06:48:02.088013+00:00 app[web.1]: at new ConnectionString (/app/node_modules/mongodb-connection-string-url/lib/index.js:86:19)
2022-07-22T06:48:02.088014+00:00 app[web.1]: at parseOptions (/app/node_modules/mongodb/lib/connection_string.js:209:17)
2022-07-22T06:48:02.088014+00:00 app[web.1]: at new MongoClient (/app/node_modules/mongodb/lib/mongo_client.js:62:63)
2022-07-22T06:48:02.088015+00:00 app[web.1]: at /app/node_modules/mongoose/lib/connection.js:796:16
2022-07-22T06:48:02.088015+00:00 app[web.1]: at new Promise (<anonymous>)
2022-07-22T06:48:02.088016+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:793:19)
2022-07-22T06:48:02.088016+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:379:10
2022-07-22T06:48:02.088016+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2022-07-22T06:48:02.088016+00:00 app[web.1]: at new Promise (<anonymous>)
2022-07-22T06:48:02.088021+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2022-07-22T06:48:02.228355+00:00 heroku[web.1]: Process exited with status 1
Below is the relevant code:
INDEX.JS
const express = require('express')
const colors = require('colors')
const { graphqlHTTP } = require('express-graphql')
const cors = require('cors')
const schema = require('./schema/schema')
const connectDB = require('./config/db')
require('dotenv').config()
const app = express()
// MongoDB
connectDB()
// CORS
app.use(cors())
// GraphQL
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
graphiql: process.env.NODE_ENV === 'development',
})
)
app.listen(
process.env.PORT || 4000,
console.log(`Server running on port ${process.env.PORT}`)
)
DB.JS
const mongoose = require('mongoose')
const connectDB = async () => {
const conn = await mongoose.connect(process.env.MONGODB_URI)
console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline.bold)
}
module.exports = connectDB
PACKAGE.JSON
{
"name": "project-management-webapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npm install && cd server && node index.js",
"dev": "nodemon server/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/BIGWALDOR/project-management-webapp.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/BIGWALDOR/project-management-webapp/issues"
},
"homepage": "https://github.com/BIGWALDOR/project-management-webapp#readme",
"dependencies": {
"colors": "^1.4.0",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-graphql": "^0.12.0",
"graphql": "^15.8.0",
"mongoose": "^6.4.5"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
I'm definitely at a loss here so any help would be much appreciated!
I'm trying to host a Discord bot for the first time through Heroku (also my first time using the site). It was running perfectly locally, but I started receiving errors in regards to the $PORT once I went online. When I looked that up, I saw I needed to listen to the server in order for the port to stick, but every attempt I've done results in the EADDRINUSE error. I've looked over and tried many examples through these previous answers here and here, deleted modules and reinstalled them countless times, ended node.exe processes before booting up, but I keep running into the error. I must be missing something but I can't figure it out and I feel like I'm just running in circles. Any help would be appreciated.
The error report:
2020-07-28T21:24:01.605923+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-28T21:24:03.399990+00:00 app[web.1]:
2020-07-28T21:24:03.400006+00:00 app[web.1]: > bot#1.0.0 start /app
2020-07-28T21:24:03.400006+00:00 app[web.1]: > node bot.js
2020-07-28T21:24:03.400006+00:00 app[web.1]:
2020-07-28T21:24:04.323323+00:00 app[web.1]: Ready!
2020-07-28T21:24:06.981374+00:00 app[web.1]: Listening on 50667
2020-07-28T21:24:07.141720+00:00 app[web.1]: events.js:292
2020-07-28T21:24:07.141721+00:00 app[web.1]: throw er; // Unhandled 'error' event
2020-07-28T21:24:07.141722+00:00 app[web.1]: ^
2020-07-28T21:24:07.141722+00:00 app[web.1]:
2020-07-28T21:24:07.141724+00:00 app[web.1]: Error: listen EADDRINUSE: address already in use :::50667
2020-07-28T21:24:07.141725+00:00 app[web.1]: at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-07-28T21:24:07.141725+00:00 app[web.1]: at listenInCluster (net.js:1361:12)
2020-07-28T21:24:07.141726+00:00 app[web.1]: at Server.listen (net.js:1447:7)
2020-07-28T21:24:07.141727+00:00 app[web.1]: at Function.listen (/app/node_modules/express/lib/application.js:618:24)
2020-07-28T21:24:07.141727+00:00 app[web.1]: at Client.<anonymous> (/app/bot.js:21075:4)
2020-07-28T21:24:07.141727+00:00 app[web.1]: at Client.emit (events.js:315:20)
2020-07-28T21:24:07.141743+00:00 app[web.1]: at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
2020-07-28T21:24:07.141745+00:00 app[web.1]: at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
2020-07-28T21:24:07.141752+00:00 app[web.1]: at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
2020-07-28T21:24:07.141752+00:00 app[web.1]: at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)
2020-07-28T21:24:07.141753+00:00 app[web.1]: Emitted 'error' event on Server instance at:
2020-07-28T21:24:07.141753+00:00 app[web.1]: at emitErrorNT (net.js:1340:8)
2020-07-28T21:24:07.141753+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-07-28T21:24:07.141754+00:00 app[web.1]: code: 'EADDRINUSE',
2020-07-28T21:24:07.141754+00:00 app[web.1]: errno: 'EADDRINUSE',
2020-07-28T21:24:07.141755+00:00 app[web.1]: syscall: 'listen',
2020-07-28T21:24:07.141755+00:00 app[web.1]: address: '::',
2020-07-28T21:24:07.141756+00:00 app[web.1]: port: 50667
2020-07-28T21:24:07.141756+00:00 app[web.1]: }
2020-07-28T21:24:07.154255+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-07-28T21:24:07.154464+00:00 app[web.1]: npm ERR! errno 1
2020-07-28T21:24:07.157313+00:00 app[web.1]: npm ERR! bot#1.0.0 start: `node bot.js`
2020-07-28T21:24:07.157445+00:00 app[web.1]: npm ERR! Exit status 1
2020-07-28T21:24:07.157646+00:00 app[web.1]: npm ERR!
2020-07-28T21:24:07.157759+00:00 app[web.1]: npm ERR! Failed at the bot#1.0.0 start script.
2020-07-28T21:24:07.157832+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-28T21:24:07.169750+00:00 app[web.1]:
2020-07-28T21:24:07.169882+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-07-28T21:24:07.169957+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-07-28T21_24_07_159Z-debug.log
2020-07-28T21:24:07.254000+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-28T21:24:07.225012+00:00 heroku[web.1]: Process exited with status 1
package.json
{
"name": "bot",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"discord.js": "^12.2.0",
"exit-hook": "^2.2.0",
"express": "^4.17.1",
"http": "0.0.1-security",
"webpack-dev-server": "^3.11.0"
},
"scripts": {
"start": "node bot.js",
"stop-win": "Taskkill /IM node.exe /F"
},
"main": "bot.js",
"devDependencies": {},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
Current code (though I have tried many different ones as well.)
const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
},);
I understand that I should probably run the bot as a Worker instead of Web through Heroku, but the option is not available to me for some reason and my Procfile was doing nothing.
I know there must be something I'm overlooking. When I run the stop-win script, it kills two node.exe processes even when I try running the code only once.
I've been looking at this for too long (days!) and keep going in circles, I need some fresh eyes and ideas. Please help!
EDIT:
Thanks Tin Nguyen for pointing me in the right direction for the Procfile! I was able to use this thread to make an echo, and thus a functioning Procfile making it a worker. This let me fix all of the mess. Thank you very much!
I think the error is comming from the port! look at the errors,
Ready!
Listening on 50667
events.js:292
throw er; // Unhandled 'error' event
Error: listen EADDRINUSE: address already in use :::50667
it says "address already in use", try to change it and let me noticied if it works!
I've been stuck on this error for a couple days now, so here I am looking for some insight. I have a PERN stack application that I am trying to deploy for the first time to Heroku. My app builds seemingly fine. My app runs perfectly fine locally in development. I can run it serving static files from react build and I can run the front and backend on two ports and it works. I think the issue has too do with how Sequelize-CLI tries to parse the DATABASE_URL config variable that Heroku sets in production. If I log process.env.DATABASE_URL I get the URI string (noted in error stack below) from Heroku.
I made sure that the PG add-on was configured correctly.
I have all my config variables set in both the .env file and heroku dashboard
My node_modules, .env file are not committed to my repo.
I have deleted my node_modules and package-lock.json and reinstalled them
I have logged out process.env.DATABASE_URL and it successfully logs the URI
I keep getting this error stack:
enter code here2020-07-19T00:56:44.000000+00:00 app[api]: Build succeeded
2020-07-19T00:57:10.671364+00:00 heroku[web.1]: Starting process with command `npm start`
2020-07-19T00:57:14.245382+00:00 app[web.1]:
2020-07-19T00:57:14.245410+00:00 app[web.1]: > locals#1.0.0 start /app
2020-07-19T00:57:14.245411+00:00 app[web.1]: > node server.js
2020-07-19T00:57:14.245411+00:00 app[web.1]:
// this is the console.log(process.env.DATABASE_URL)
2020-07-19T01:09:25.213184+00:00 app[web.1]: postgres://<user>:<password><omitted-info>:<port>/<app>
2020-07-19T00:57:14.903836+00:00 app[web.1]: internal/validators.js:107
2020-07-19T00:57:14.903838+00:00 app[web.1]: throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
2020-07-19T00:57:14.903839+00:00 app[web.1]: ^
2020-07-19T00:57:14.903839+00:00 app[web.1]:
2020-07-19T00:57:14.903841+00:00 app[web.1]: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined
2020-07-19T00:57:14.903841+00:00 app[web.1]: at validateString (internal/validators.js:107:11)
2020-07-19T00:57:14.903841+00:00 app[web.1]: at Url.parse (url.js:155:3)
2020-07-19T00:57:14.903842+00:00 app[web.1]: at Object.urlParse [as parse] (url.js:150:13)
2020-07-19T00:57:14.903843+00:00 app[web.1]: at new Sequelize (/app/node_modules/sequelize/lib/sequelize.js:187:28)
2020-07-19T00:57:14.903843+00:00 app[web.1]: at Object.<anonymous> (/app/models/index.js:14:15)
2020-07-19T00:57:14.903844+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:759:30)
2020-07-19T00:57:14.903844+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
2020-07-19T00:57:14.903845+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:628:32)
2020-07-19T00:57:14.903845+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:555:12)
2020-07-19T00:57:14.903845+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:666:19)
2020-07-19T00:57:14.903846+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:16:16)
2020-07-19T00:57:14.903846+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:11:16)
2020-07-19T00:57:14.903847+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:759:30)
2020-07-19T00:57:14.903847+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
2020-07-19T00:57:14.903847+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:628:32)
2020-07-19T00:57:14.903848+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:555:12)
2020-07-19T00:57:14.903848+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
2020-07-19T00:57:14.903849+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2020-07-19T00:57:14.915610+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-07-19T00:57:14.916028+00:00 app[web.1]: npm ERR! errno 1
2020-07-19T00:57:14.917598+00:00 app[web.1]: npm ERR! locals#1.0.0 start: `node server.js`
2020-07-19T00:57:14.917834+00:00 app[web.1]: npm ERR! Exit status 1
2020-07-19T00:57:14.918113+00:00 app[web.1]: npm ERR!
2020-07-19T00:57:14.918349+00:00 app[web.1]: npm ERR! Failed at the locals#1.0.0 start script.
2020-07-19T00:57:14.918598+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-19T00:57:14.952443+00:00 app[web.1]:
2020-07-19T00:57:14.952709+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-07-19T00:57:14.952798+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-07-19T00_57_14_920Z-debug.log
2020-07-19T00:57:15.047526+00:00 heroku[web.1]: Process exited with status 1
2020-07-19T00:57:15.094041+00:00 heroku[web.1]: State changed from starting to crashed
2020-07-19T00:57:15.951511+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=locals-deploy.herokuapp.com request_id=f27a1f1b-5d62-4d98-9383-6d5e46242789 fwd="71.92.89.136" dyno= connect= service= status=503 bytes= protocol=https
Now if I try to run a db:migrate on heroku in production mode I get another error regarding the URL.
heroku run sequelize db:migrate --env production --app locals-deploy
Error:
Running sequelize db:migrate on ⬢ locals-deploy... up, run.7436 (Free)
Sequelize CLI [Node: 12.0.0, CLI: 5.5.1, ORM: 5.22.3]
Loaded configuration file "config/config.js".
Using environment "production".
ERROR: Error parsing url: undefined
package.json
{
"name": "locals",
"version": "1.0.0",
"description": "A place for travelers to meet locals.",
"main": "server.js",
"scripts": {
"start": "node server.js",
"heroku-postbuild": "cd client && npm install && npm run build"
},
"author": "Name Name",
"license": "MIT",
"dependencies": {
"axios": "^0.19.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cloudinary": "^1.22.0",
"config": "^3.3.1",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-fileupload": "^1.1.7-alpha.3",
"express-validator": "^6.5.0",
"jsonwebtoken": "^8.5.1",
"nodemon": "^2.0.4",
"pg": "^8.2.1",
"pg-hstore": "^2.3.3",
"sequelize": "^5.21.11",
"sequelize-cli": "^5.5.1"
},
"engines": {
"node": "12.0.0"
}
}
server.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 5000;
const upload = require('express-fileupload');
app.use(upload({useTempFiles: true}));
const cors = require('cors');
const path = require('path');
const models = require('./models');
// variable to enable global error logging
const enableGlobalErrorLogging =
process.env.ENABLE_GLOBAL_ERROR_LOGGING === 'true';
// Enable All CORS Requests
app.use(cors());
//Init Middleware
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
//app.use(express.static(path.join(__dirname, 'client/build')));
console.log(process.env.NODE_ENV);
//if running in production mode then it serves static files from build in client
if (process.env.NODE_ENV === 'production') {
//points to index.js in client
app.use(express.static(path.join(__dirname, 'client/build')));
}
//Routes
app.use('/users', require('./routes/users'));
app.use('/auth', require('./routes/auth'));
app.use('/posts', require('./routes/posts'));
app.use('/api/profile', require('./routes/profile'));
app.use('/adventure', require('./routes/adventure'));
app.use('/review', require('./routes/review'));
app.use('/favorites', require('./routes/favorites'));
app.use('/upload', require('./routes/uploadImage'));
//catch all method redirects to build folder
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
// send 404 if no other route matched
app.use((req, res) => {
res.status(404).json({
message: 'Route Not Found',
});
});
// setup a global error handler
app.use((err, req, res, next) => {
console.log('I am catching the error');
if (enableGlobalErrorLogging) {
console.error(`Global error handler: ${JSON.stringify(err.stack)}`);
}
if (err.name === 'SequelizeUniqueConstraintError') {
return res.status(400).json({
errors: ['Email for user already exists'],
});
}
// if (err.name === 'SequelizeDatabaseError') {
// return res.status(404).json({errors: 'Oh no! Page not found.'});
// }
res.status(err.status || 500).json({
message: err.message,
name: err.name,
error: {},
});
});
//Sets Port and Listens
return models.sequelize.sync().then((result) => {
app.listen(process.env.PORT || 5000, () => {
console.log(`App running on port ${process.env.PORT || 5000}.`);
});
});
config.js
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
//PG_HOST = 127.0.0.1
console.log(process.env.DATABASE_URL);
module.exports = {
development: {
username: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
host: process.env.PG_HOST,
dialect: 'postgres',
port: process.env.PG_PORT,
operatorsAliases: 0,
},
test: {
username: process.env.PG_USER,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
host: process.env.PG_HOST,
dialect: 'postgres',
port: process.env.PG_PORT,
operatorsAliases: 0,
},
production: {
use_env_variable: process.env.DATABASE_URL,
dialect: 'postgres',
dialectOptions: {
ssl: true,
},
},
};
Procfile
web: node server.js
EDIT
I made a bug report and I actually think I may have fixed the issue in the node_modules package where the error was being thrown. Now I have to figure out how to make that change inside Heroku since we don't commit node_modules to see if it works. I can run it just find locally now in production mode without reproducing the error.
Bug Report if anyone is interested
https://github.com/sequelize/sequelize/issues/12528
Try this in your config file
production: {
use_env_variable: "DATABASE_URL",
dialect: 'postgres',
dialectOptions: {
ssl: true,
},
},
Since you are using use_env_variable you should be able to just have the name of the variable as the value.
So as it turns out there is an error in my node_modules/sequelize package. I traced into into /node_modules/sequelize/lib/sequelize.js:187:28. Whats happening is that my config file is submitting the URI string under production from process.env.DATABASE_URL and it is attached to the username parameter, or index 1. The issue is how url.parse is written and what argument is passed. If the user submits the DATABASE_URL URI it passed as if it is an array, however, the first argument is always undefined since its username that it is attached to, which is index 1. I changed the parsing arguments to const urlParts = url.parse(options.use_env_variable, true); and this seemed to clear up all my issues by directly taking the URI to be parsed instead of worrying about index values.
constructor(database, username, password, options) {
let config;
if (arguments.length === 1 && typeof database === 'object') {
// new Sequelize({ ... options })
options = database;
config = _.pick(options, 'host', 'port', 'database', 'username', 'password');
} else if (
(arguments.length === 1 && typeof database === 'string') ||
(arguments.length === 2 && typeof username === 'object')
) {
// new Sequelize(URI, { ... options }) <--this was already commented out
config = {};
options = username || {};
// url.parse wants to access the object like an array, however, first item is always undefined and it
// needs to access the use_env_variable, which is attached to username argument
// const urlParts = url.parse(options.use_env_variable, true);
const urlParts = url.parse(arguments[0], true);
options.dialect = urlParts.protocol.replace(/:$/, '');
options.host = urlParts.hostname;
...
After that I downloaded npm patch-package and I pushed my patched package to git and heroku and it works.
I'm relatively new to node, and have been having issues deploying a node web app to heroku... it seems to have to do with how I'm creating my mongoose connection in my server file. Here are the error logs I'm getting:
2016-01-06T00:41:30.384170+00:00 heroku[web.1]: State changed from starting to crashed
2016-01-06T00:43:56.191644+00:00 heroku[web.1]: State changed from crashed to starting
2016-01-06T00:43:57.774259+00:00 heroku[web.1]: Starting process with command `node server/server.js`
2016-01-06T00:44:00.138974+00:00 app[web.1]: listening on 4568
2016-01-06T00:44:00.172982+00:00 app[web.1]:
2016-01-06T00:44:00.172992+00:00 app[web.1]: /app/node_modules/mongoose/node_modules/mongodb/lib/server.js:236
2016-01-06T00:44:00.172994+00:00 app[web.1]: process.nextTick(function() { throw err; })
2016-01-06T00:44:00.172994+00:00 app[web.1]: ^
2016-01-06T00:44:00.172995+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2016-01-06T00:44:00.172996+00:00 app[web.1]: at Object.exports._errnoException (util.js:856:11)
2016-01-06T00:44:00.172997+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:879:20)
2016-01-06T00:44:00.172998+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2016-01-06T00:44:00.968046+00:00 heroku[web.1]: Process exited with status 1
2016-01-06T00:44:00.983259+00:00 heroku[web.1]: State changed from starting to crashed
And here is a shell of my server file:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var entryController = require('./entries/entryController.js');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(__dirname + '/../client'));
mongoose.connect('mongodb://localhost');
console.log('listening on 4568');
app.listen(4568);
Any advice would be greatly appreciated, thanks!
You're trying to connect to localhost:
mongoose.connect('mongodb://localhost');
Localhost means "this computer." Your database is not running in the same container on Heroku as your node app, so instead you need to connect to where your database really is hosted.
You may want to read a tutorial like:
https://scotch.io/tutorials/use-mongodb-with-a-node-application-on-heroku
I've recently been trying to learn both Node.js and Mongo. I set up an account on Heroku, got a simple Node.js app running correctly and then took a dive into Mongo. I added on MongoHQ, but since then have been unable to connect the app to the database. Every time I run the app it crashes. At this point I just want to figure out what is wrong with my db connection or my use of mongoskin. Here is my current code:
var express = require('express');
var mongo = require('mongoskin');
var app = express.createServer(express.logger());
var conn = mongo.db(process.env.MONGOHQ_URL);
app.get('/', function(request, response) {
conn.collection('facts').find().toArray(function(err, facts){
if(err) throw err;
response.send(facts);
});
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
and my package file:
{
"name": "app1",
"version": "0.0.4",
"dependencies": {
"express": "3.x",
"mongoskin" : "0.1.x"
},
"engines": {
"node": "0.8.x",
"npm": "1.1.x"
}
}
and lastly the error code:
2012-12-11T17:42:32+00:00 app[web.1]: at Module.load (module.js:356:32)
2012-12-11T17:42:32+00:00 app[web.1]: at Module._compile (module.js:449:26)
2012-12-11T17:42:32+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/mongoskin/lib/mongoskin/db.js:162:28)
2012-12-11T17:42:32+00:00 app[web.1]: at Db.Object.defineProperty.get [as state] (/app/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/db.js:2125:31)
2012-12-11T17:42:32+00:00 app[web.1]: at Module.require (module.js:362:17)
2012-12-11T17:42:32+00:00 app[web.1]: at Module._compile (module.js:449:26)
2012-12-11T17:42:32+00:00 app[web.1]: TypeError: Cannot read property '_serverState' of undefined
2012-12-11T17:42:32+00:00 app[web.1]: at Function.Module._load (module.js:312:12)
2012-12-11T17:42:32+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/mongoskin/lib/mongoskin/index.js:2:10)
2012-12-11T17:42:32+00:00 app[web.1]:
2012-12-11T17:42:32+00:00 app[web.1]: at Object.Module._extensions..js (module.js:467:10)
2012-12-11T17:42:32+00:00 app[web.1]: at require (module.js:378:17)
2012-12-11T17:42:33+00:00 heroku[web.1]: Process exited with status 1
2012-12-11T17:42:33+00:00 heroku[web.1]: State changed from starting to crashed
Any help on what might be causing the problem would be highly appreciated.
You need to just update mongoskin library. Replace this line:
"mongoskin" : "0.1.x"
with
"mongoskin" : ">= 0.3.6"
More details here