59

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.

9 Answers 9

137

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I had this same problem and had been trying to figure it out for the longest time. Thank you and than @dankohn!
YOU SIR ARE A FREAKING LEGEND. Had this wrong in the (auto generated) package.json file with „server.js“ instead of „Server.js“.
Bravo, this was just the ticket for me and my "Tools"/"tools" folder
Wow - Thank you - had no idea what was going on - I had misnamed my models User file with lower case user.js on my initial push to heroku and even though I corrected it later on - the file stayed lower case
this worked for me too I checked with Heroku run bash and my file was somehow capital and locally it was lowercase. thanks a lot
|
37

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.

4 Comments

Thanks for this helpful debugging tip which enabled me to fix the issue.
I never knew you could look at your heroku file structure - this has been a life saver - Thanks!!!
still valid in 2021
What key words in REPL should I type?
7

For me, it was caused by a folder that I had accidentally included in .gitignore!

Comments

2

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

1 Comment

This didn't work for me. If you are working on MacOS and changed the case of a file's name, the only solution that worked for me is the first one, i.e. rename the file into a temporary name that differs not only by the letter's case, and then change it back... Thought it's worth mentioning to save others the time of testing this (BTW cache is a good thing, and I wouldn't waive it that easily)
2

Not sure if this is the same issue as described here, but for me my require("dotenv").config() was not conditioned upon the environment that the code was running in, thus Heroku could not find it since it was installed as a devDependency.

Fix:

if (process.env.NODE_ENV !== "production") {
  require("dotenv").config();
}

Comments

0

One of my files had a lowercase name locally and it was required as a lowercase.

const Product = require('../models/product');

On the git repo it was capitalized.

'../models/Product'

The server was trying to require a file which did not exist. I had to use git mv to rename the file locally then reupload it to fix the issue.

Comments

0

For me, I just deleted the older app from Heroku and created the new one via Heroku web, and then pushed the code to the newer one, and then it worked.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
0

For me what I changed was:

  • File name was CheckPermissions and I changed it to checkPermissions and then hosted. Error occurred.
  • Then revert the changes and hosted. This time worked well.

Comments

0

I faced the same issue and resolved using dockerizing my application.

  1. Create dockerFile from node
  2. set heroku stack as docker
  3. Deploy

Ref : https://devcenter.heroku.com/categories/deploying-with-docker

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.