Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku + Node: Cannot find module error

My Node app is running fine locally, but has run into an error when deploying to Heroku. The app uses Sequelize in a /models folder, which contains index.js, Company.js and Users.js. Locally, I am able to import the models using the following code in /models/index.js:

// load models var models = [   'Company',   'User' ]; models.forEach(function(model) {   module.exports[model] = sequelize.import(__dirname + '/' + model); }); 

This works fine, however, when I deploy to Heroku the app crashes with the following error:

Error: Cannot find module '/app/models/Company'    at Function.Module._resolveFilename (module.js:338:15)    at Function.Module._load (module.js:280:25)    at Module.require (module.js:364:17)    at require (module.js:380:17)    at module.exports.Sequelize.import (/app/node_modules/sequelize/lib/sequelize.js:219:24)    at module.exports.sequelize (/app/models/index.js:60:43)    at Array.forEach (native)    at Object.<anonymous> (/app/models/index.js:59:8)    at Module._compile (module.js:456:26)    at Object.Module._extensions..js (module.js:474:10) Process exited with status 8 

Initially I thought it was due to case sensitivity (local mac vs heroku linux), but I moved the file, made a git commit, and then moved back and committed again to ensure Company.js is capitalized in the git repository. This didn't solve the problem and I'm not sure what the issue could be.

like image 379
surfearth Avatar asked Oct 13 '13 04:10

surfearth


People also ask

Why is my Heroku app not working?

There are some errors which only occur when the app is rebooting so you will need to restart the app to see these log messages appear. For most apps, we also recommend enabling one of the free logging addons from https://elements.heroku.com/addons#logging to make sure that your historical log data is being saved.

Why is my Heroku build failing?

Sometimes installing a dependency or running a build locally completes successfully, and when it gets to Heroku, the build will fail. If you run into an issue where a dependency only fails on Heroku, it's recommended to spin up a one-off dyno to debug the script.

How do you run NPM install on Heroku?

Run the npm install command in your local app directory to install the dependencies that you declared in your package. json file. Start your app locally using the heroku local command, which is installed as part of the Heroku CLI. Your app should now be running on http://localhost:5000/.


2 Answers

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.

like image 165
surfearth Avatar answered Sep 24 '22 10:09

surfearth


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.

like image 45
Dan Kohn Avatar answered Sep 23 '22 10:09

Dan Kohn