'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.



Solution 1:[1]

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 2:[2]

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

Solution 3:[3]

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 4:[4]

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 5:[5]

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 6:[6]

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 7:[7]

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 8:[8]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Kohn
Solution 2 Adrien Joly
Solution 3 timarcosdias
Solution 4 Gustavo Maximo
Solution 5 lbragile
Solution 6 Iam_vgs
Solution 7 Mohit Saud
Solution 8 Ranjith Raj D