CollinKleest
CollinKleest

Reputation: 21

Heroku cannot find mongoose module after deployment?

I have deployed a simple Nodejs app to Heroku and I keep getting an error in the application logs saying that the module "mongoose" cannot be found. I even provided a relative path instead of just saying require("mongoose"). The deployment succeeds and installs all the dependencies. The source code can be found here https://github.com/collinkleest/trim-io.

Here is the error log from heroku.

2020-10-17T02:01:42.000000+00:00 app[api]: Build started by user [email protected]
2020-10-17T02:01:59.000000+00:00 app[api]: Build succeeded
2020-10-17T02:01:59.493929+00:00 app[api]: Deploy af985be5 by user [email protected]
2020-10-17T02:01:59.493929+00:00 app[api]: Release v14 created by user [email protected]
2020-10-17T02:01:59.652847+00:00 heroku[web.1]: State changed from crashed to starting
2020-10-17T02:02:01.917601+00:00 heroku[web.1]: Starting process with command `npm start`
2020-10-17T02:02:04.175589+00:00 app[web.1]:
2020-10-17T02:02:04.175612+00:00 app[web.1]: > [email protected] start /app
2020-10-17T02:02:04.175612+00:00 app[web.1]: > node server.js
2020-10-17T02:02:04.175613+00:00 app[web.1]:
2020-10-17T02:02:04.369536+00:00 app[web.1]: internal/modules/cjs/loader.js:834
2020-10-17T02:02:04.369538+00:00 app[web.1]: throw err;
2020-10-17T02:02:04.369539+00:00 app[web.1]: ^
2020-10-17T02:02:04.369539+00:00 app[web.1]:
2020-10-17T02:02:04.369539+00:00 app[web.1]: Error: Cannot find module '../node_modules/mongoose'
2020-10-17T02:02:04.369540+00:00 app[web.1]: Require stack:
2020-10-17T02:02:04.369540+00:00 app[web.1]: - /app/models/urlEntity.js
2020-10-17T02:02:04.369541+00:00 app[web.1]: - /app/server.js
2020-10-17T02:02:04.369541+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:831:15)
2020-10-17T02:02:04.369542+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:687:27)
2020-10-17T02:02:04.369542+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:903:19)
2020-10-17T02:02:04.369543+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2020-10-17T02:02:04.369543+00:00 app[web.1]: at Object.<anonymous> (/app/models/urlEntity.js:1:18)
2020-10-17T02:02:04.369543+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1015:30)
2020-10-17T02:02:04.369544+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
2020-10-17T02:02:04.369544+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:879:32)
2020-10-17T02:02:04.369545+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:724:14)
2020-10-17T02:02:04.369545+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:903:19) {
2020-10-17T02:02:04.369545+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2020-10-17T02:02:04.369546+00:00 app[web.1]: requireStack: [ '/app/models/urlEntity.js', '/app/server.js' ]
2020-10-17T02:02:04.369546+00:00 app[web.1]: }
2020-10-17T02:02:04.389361+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-10-17T02:02:04.389827+00:00 app[web.1]: npm ERR! errno 1
2020-10-17T02:02:04.398788+00:00 app[web.1]: npm ERR! [email protected] start: `node server.js`
2020-10-17T02:02:04.399025+00:00 app[web.1]: npm ERR! Exit status 1
2020-10-17T02:02:04.399280+00:00 app[web.1]: npm ERR!
2020-10-17T02:02:04.399493+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2020-10-17T02:02:04.399722+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-10-17T02:02:04.420468+00:00 app[web.1]:
2020-10-17T02:02:04.420707+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-10-17T02:02:04.420857+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-10-17T02_02_04_400Z-debug.log
2020-10-17T02:02:04.487055+00:00 heroku[web.1]: Process exited with status 1
2020-10-17T02:02:04.554273+00:00 heroku[web.1]: State changed from starting to crashed

Here is my server.js for reference as well:

const express = require("express");
const UrlEntity = require("./models/urlEntity"); 
const mongoose = require("./node_modules/mongoose");
const urlEntity = require("./models/urlEntity");
const app = express();
require('dotenv').config();

const mongoUri = "mongodb+srv://" + process.env.MONGO_USR + ":" + process.env.MONGO_PASS + "@" + process.env.MONGO_URL + "/" + process.env.MONGO_DB + "?retryWrites=true&w=majority";

mongoose.connect(
    mongoUri,
    { useNewUrlParser: true, useUnifiedTopology: true}
);

app.use(express.json());
app.use(express.static('public'));

async function checkIdExists(id){
    let exists = await UrlEntity.exists({uniqueId: id});
    return exists;
}

function generateId(){
    let randomId = Math.random().toString(20).substr(2, 8);
    while(!(checkIdExists(randomId))){
        randomId = Math.random().toString(20).substr(2, 8);
    }
    return randomId;
}

// redirect functionality 
app.get("/:id", async (req, res) => {
    if (req.params.id !== null || req.params.id !== undefined){
        let doc = await UrlEntity.findOne({uniqueId: req.params.id});
        res.redirect(doc.targetUrl);
    } else {
        res.redirect('/');
    }
});

// create new url
app.post("/create-url", async (req, res) => {
    console.log("in post");
    let id = generateId();
    await UrlEntity.create({uniqueId: id,
    targetUrl: req.body.url,
    dateCreated: req.body.date});
    res.send(`${req.protocol}://${req.get('host')}/${id}`);
});

app.listen(process.env.PORT || 5000,  () => {console.log("Trim.io API Started")});

Upvotes: 0

Views: 233

Answers (2)

prince Arthur
prince Arthur

Reputation: 231

  • try changing the const mongoose = require("./node_modules/mongoose") back to const mongoose = require("mongoose");

  • Don't forget to set your environment variables MONGO_USR, MONGO_PASS e.t.c on heroku settings. Your server will not start if the environment variable is not set.

  • Lastly I advice you use a connection string like this mongodb+srv://user:[email protected]/databaseName without concatenating

Upvotes: 0

Danielle Adams
Danielle Adams

Reputation: 260

You put mongoose in your devDependencies, which will be removed at runtime. (Link from your code) You need to move it to dependencies so that it will be available at runtime.

Upvotes: 1

Related Questions