简体   繁体   中英

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.

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.

我看不到确切的修复方法,但是您可以通过运行heroku run bash登录到 Heroku 实例,然后运行node以输入 REPL,并尝试直接要求路径来自己解决。

对我来说,这是由我不小心包含在 .gitignore 中的文件夹引起的!

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM