1

When I deployed my Node project that uses express/mongoose to Heroku and looked at the logs it showed

Error: Cannot find module '../models/Posts'

Which is required in my index.js file as

const Posts = require('../models/Posts');

This path works for me when testing on a local server. I can temporarily fix this by moving the model to the same folder as index.js and changing the path to const Posts = require('./models/Posts');.

It looks like Heroku is having trouble interpreting the ../’s in my path correctly. Is there a fix for this?

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
Brendan
  • 11
  • 4
  • In general you should never program using relative paths (aka: `../` and `../../` and the like) and always use absolute paths to your assets and content. Unsure how you app is structured or looks like, but that is really the best solution for NodeJS and tons of frameworks and even non-framework based programming. – Giacomo1968 May 01 '19 at 00:22
  • I tried changing the route to `"/app/models/Posts"` but I still get `Error: Cannot find module '/app/models/Posts'` – Brendan May 01 '19 at 00:59

1 Answers1

0

It turns out that I had a bunch of console.log()s in my routes(index.js). Removing them fixed the problem.

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
Brendan
  • 11
  • 4
  • Well, this might be *a* thing that fixed this issue, but it honestly sounds a bit crazy that logging to console would cause issues like this. Most likely your `console.log()` broke or delayed something in the `index.js`. – Giacomo1968 May 01 '19 at 13:41
  • 1
    It is strange considering my app is working on heroku now with a console.log() used in a catch to return an error. Maybe there was a console.log() that was trying to log something undefined. None the less its strange that somehow that was preventing heroku from reading the module. – Brendan May 01 '19 at 18:16