Heroku + Node: Cannot find module error

JavascriptGitnode.jsHerokusequelize.js

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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();
}

Solution 7 - Javascript

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.

Solution 8 - Javascript

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.

Solution 9 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsurfearthView Question on Stackoverflow
Solution 1 - JavascriptsurfearthView Answer on Stackoverflow
Solution 2 - JavascriptDan KohnView Answer on Stackoverflow
Solution 3 - JavascriptAdrien JolyView Answer on Stackoverflow
Solution 4 - JavascripttimarcosdiasView Answer on Stackoverflow
Solution 5 - JavascriptGustavo MaximoView Answer on Stackoverflow
Solution 6 - JavascriptlbragileView Answer on Stackoverflow
Solution 7 - JavascriptIam_vgsView Answer on Stackoverflow
Solution 8 - JavascriptMohit SaudView Answer on Stackoverflow
Solution 9 - JavascriptRanjith Raj DView Answer on Stackoverflow