skip to Main Content

For some odd reason (i have been deployed on Heroku for around 1.5 years) my instance decided to throw a weird error regarding not finding ‘mongoose’ after attempting to deploy again. Everything works on my local server and my .gitignore file ignores Node Modules. This is a React.js app with Expressjs and Mongoose.

Here is my Package.json:

    {
  "name": "drg-coming-soon",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "server": "node server/index.js",
    "start": "webpack && node server/index.js",
    "react": "webpack -d --watch"
  },
  "heroku-run-build-script": true,
  "author": "Randy Thomas",
  "license": "ISC",
  "devDependencies": {
    "axios": "^0.18.0",
    "babel-core": "^6.23.1",
    "babel-loader": "^6.3.2",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.23.0",
    "body-parser": "^1.18.3",
    "express": "^4.15.0",
    "jquery": "^3.1.1",
    "mongoose": "^6.2.7",
    "react-awesome-modal": "^2.0.5",
    "request": "^2.81.0",
    "webpack": "^4.28.3"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "css-loader": "^2.0.1",
    "dotenv": "^6.2.0",
    "file-loader": "^2.0.0",
    "react": "^15.4.2",
    "react-autosuggest": "^9.4.3",
    "react-dom": "^15.4.2",
    "react-image": "^1.5.1",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "^2.1.1",
    "style-loader": "^0.23.1",
    "twilio": "^3.33.2",
    "url-loader": "^1.1.2",
    "mongoose": "^6.2.7",
    "webpack": "^4.28.3"
  }
}

Here is my webpack.config.js

const path = require('path');

const SRC_DIR = path.join(__dirname, '/client/src');
const DIST_DIR = path.join(__dirname, '/client/dist');

module.exports = {
  entry: `${SRC_DIR}/index.js`,
  output: {
    filename: 'bundle.js',
    path: DIST_DIR,
  },
  module: {
    //changed from loaders to rules
    loaders: [
      {
        test: /.jsx?/,
        include: SRC_DIR,
        loader: 'babel-loader',
        query: {
          presets: [
            '@babel/preset-env',
            '@babel/preset-react'
          ],
        },
      },
    ],
  },
};

Here is my Database folder:

require('dotenv').config();
const mongoose = require('mongoose');
// dev

// process.env.mongourl

mongoose.connect(process.env.mongourl)
  .catch(err => console.log('Mongo connection error', err));


const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
  console.log('MongoDB has connected');
});

// schemas
const comingSoonSchema = ({
  address: String,
  desc: String,
  sqft: Number,
  bed: String,
  bath: String,
  photoLink: String,
  agent: String,
  price: String,
  year: Number,
  eta: String,
  premarket: String,
  status: String,
  timeStamp: { type: Date, default: Date.now },
})


// models
const ComingSoon = mongoose.model('ComingSoon', comingSoonSchema);

function save(e) {
  console.log(e, "SAVE FUNC");
  const obj = new ComingSoon({
    address: e.address,
    desc: e.desc,
    sqft: e.sqft,
    bed: e.bed,
    bath: e.bath,
    photoLink: e.photoLink,
    agent: e.agent,
    price: e.price,
    year: e.year,
    eta: e.eta,
    status: e.status,
    premarket: e.premarket
  })
  obj.save();
  console.log("Data saved to MongoDB Database");
}

const funcs = {
  save, ComingSoon,
};
module.exports = funcs;

And lastly here is the persisting error:

2022-03-23T01:33:06.685994+00:00 heroku[web.1]: Starting process with command `npm start`
2022-03-23T01:33:07.919434+00:00 app[web.1]:
2022-03-23T01:33:07.919447+00:00 app[web.1]: > [email protected] start
2022-03-23T01:33:07.919448+00:00 app[web.1]: > webpack && node server/index.js
2022-03-23T01:33:07.919448+00:00 app[web.1]:
2022-03-23T01:33:07.965317+00:00 app[web.1]: One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
2022-03-23T01:33:07.965319+00:00 app[web.1]: - webpack-cli (https://github.com/webpack/webpack-cli)
2022-03-23T01:33:07.965319+00:00 app[web.1]: The original webpack full-featured CLI.
2022-03-23T01:33:07.965784+00:00 app[web.1]: We will use "npm" to install the CLI via "npm install -D".
2022-03-23T01:33:08.131129+00:00 app[web.1]: Do you want to install 'webpack-cli' (yes/no): node:internal/modules/cjs/loader:936
2022-03-23T01:33:08.131131+00:00 app[web.1]: throw err;
2022-03-23T01:33:08.131131+00:00 app[web.1]: ^
2022-03-23T01:33:08.131132+00:00 app[web.1]:
2022-03-23T01:33:08.131132+00:00 app[web.1]: Error: Cannot find module 'mongoose'
2022-03-23T01:33:08.131132+00:00 app[web.1]: Require stack:
2022-03-23T01:33:08.131133+00:00 app[web.1]: - /app/database/index.js
2022-03-23T01:33:08.131136+00:00 app[web.1]: - /app/server/index.js
2022-03-23T01:33:08.131136+00:00 app[web.1]: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-03-23T01:33:08.131137+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-03-23T01:33:08.131137+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-03-23T01:33:08.131138+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)
2022-03-23T01:33:08.131138+00:00 app[web.1]: at Object.<anonymous> (/app/database/index.js:2:18)
2022-03-23T01:33:08.131139+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1103:14)
2022-03-23T01:33:08.131139+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
2022-03-23T01:33:08.131140+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:981:32)
2022-03-23T01:33:08.131140+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-03-23T01:33:08.131140+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1005:19) {
2022-03-23T01:33:08.131141+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-03-23T01:33:08.131141+00:00 app[web.1]: requireStack: [ '/app/database/index.js', '/app/server/index.js' ]
2022-03-23T01:33:08.131142+00:00 app[web.1]: }
2022-03-23T01:33:08.265674+00:00 heroku[web.1]: Process exited with status 1
2022-03-23T01:33:08.447674+00:00 heroku[web.1]: State changed from starting to crashed
2022-03-23T01:32:58.202124+00:00 app[api]: Release v158 created by user [email protected]
2022-03-23T01:32:58.202124+00:00 app[api]: Deploy ad551b5e by user [email protected]
2022-03-23T01:33:00.927332+00:00 app[api]: Deploy ad551b5e by user [email protected]
2022-03-23T01:33:00.927332+00:00 app[api]: Release v159 created by user [email protected]
2022-03-23T01:33:51.331557+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=drgcomingsoonlistings.herokuapp.com request_id=fd27788b-e166-49fd-8830-b0da493e7e62 fwd="47.45.81.207" dyno= connect= service= status=503 bytes= protocol=https
2022-03-23T01:33:52.166634+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=drgcomingsoonlistings.herokuapp.com request_id=4967681e-a5a9-41b9-9129-65258f9b9983 fwd="47.45.81.207" dyno= connect= service= status=503 bytes= protocol=https

2

Answers


  1. The error is most likely coming from the fact that you have mongoose in your "devDependencies". Try moving it to your "dependencies", then running npm install. You may run into the same issue with express and a few others under "devDependencies". Hope this helps.

    Login or Signup to reply.
  2. It does not make any sense, but if mongoose is both in dev dependencies and in dependencies, heroku goes nuts. It can be ONLY in dependencies. After deleting it from dev dependencies, do npm install. It will work without any issues.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search