Heroku + Node: Cannot find module error
Solution 1:
The problem was due to case sensitivity and file naming. Mac OS X is case insensitive (but aware) whereas Heroku is based on Linux and is case sensitive. By running heroku run bash
from my terminal, I was able to see how the /models
folder appeared on Heroku's file system. The solution was to rename User.js
and Company.js
on my local system to new temporary files, commit the changes to git, then rename back to User.js
and Company.js
being mindful of the capitalized first letter and then committing the changes again via git. Previously I had tried to rename the files directly from user.js
to User.js
and company.js
to Company.js
but the git commit and case-sensitive file name changes did not reflect on Heroku.
Solution 2:
I can't see the exact fix, but you can figure it out yourself by running heroku run bash
to log into a Heroku instance, then run node
to enter a REPL, and try requiring the paths directly.
Solution 3:
For me, it was caused by a folder that I had accidentally included in .gitignore!
Solution 4:
I've been through an error like this one and the cause was that I renamed a module module.js
to Module.js
and the Heroku cache was conflicting the names. You must disable module caching to avoid this kind of error:
$ heroku config:set NODE_MODULES_CACHE=false
Source: https://help.heroku.com/TO64O3OG/cannot-find-module-in-node-js-at-runtime