3

I keep receiving this error: "Error: Cannot find module 'models'" on Heroku. I'm using Node.JS. I'm using this Node.JS project.

I copied out the logs from Heroku and put them in a gist due to length.

Here's an excerpt:

2013-08-27T12:56:27.792568+00:00 app[web.1]: module.js:340
2013-08-27T12:56:27.791930+00:00 app[web.1]: 
2013-08-27T12:56:27.792873+00:00 app[web.1]:     throw err;
2013-08-27T12:56:27.792873+00:00 app[web.1]:           ^
2013-08-27T12:56:27.794658+00:00 app[web.1]:     at Function.Module._load (module.js:280:25)
2013-08-27T12:56:27.794658+00:00 app[web.1]: Error: Cannot find module 'models'

I setup my configs on Heroku and have a mongolab mongodb setup and the config val MONGO_URL is setup and the NODE_ENV is set to production.

You can find the site here.

Does anyone know what's going on? Thank you for your help!

Jason Shultz
  • 940
  • 1
  • 19
  • 36

3 Answers3

14

You need to set NODE_PATH=lib to heroku's configuration variables.

NODE_PATH tells node where to find modules besides node_modules directory.

Also, you need to tell the app to read env variables in a production environment.

So...

$ heroku config:set NODE_ENV=production
$ heroku config:set NODE_PATH=lib

There is no need to modify any file for a heroku deployment.

Cristian Douce
  • 3,148
  • 1
  • 16
  • 17
0

You are missing "models" in the "dependencies" property of your package.json. (I checked the github repo).

EDIT: sorry, looks like you are trying to use the module under /lib/models/ folder. So in this line https://github.com/gravityonmars/nodejs-starter/blob/master/index.js#L20 you can do this instead:

require('./lib/models')(app);

Gus
  • 596
  • 4
  • 7
  • So, I tried "model" but got errors once i ran sudo npm install. I tried "models" instead of "model" but that was a no go. I even tried "mongoose-models" Here's a copy of the log I get when I try to install 'models': https://gist.github.com/jshultz/5eceeb6d66444fe406e3 – Jason Shultz Aug 28 '13 at 15:40
  • Well, there's obviously more issues going on in this. I ended up adding ./lib/ in front of models, auth and boot. Once I did that it found new errors: https://gist.github.com/jshultz/22a33a86da6d0841bcab So, the question is: is it because there is a pathing issue when deployed to heroku or something else? Like I said, it works on my local machine but not when it goes to Heroku. It's a mystery. I'm giving you credit for the correct answer because you were spot on. There's just something else going on in the app. – Jason Shultz Aug 28 '13 at 17:55
  • I found the solution: heroku config:set NODE_PATH=lib – Jason Shultz Aug 28 '13 at 18:31
0

You just need to re-install models.

Do npm install models --save then re-deploy your app to Heroku.

This should solve your problem.

glennsl
  • 28,186
  • 12
  • 57
  • 75
rmrln
  • 1