Heroku + Node:找不到模块错误 [英] Heroku + Node: Cannot find module error

查看:275
本文介绍了Heroku + Node:找不到模块错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Node应用程序在本地运行良好,但在部署到Heroku时出现错误。该应用在 / models 文件夹中使用Sequelize,其中包含 index.js Company。 js Users.js 。在本地,我可以使用 /models/index.js 中的以下代码导入模型:

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

但是,当我部署到Heroku时,此应用崩溃,出现以下错误:

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

最初我以为这是由于区分大小写(本地mac vs heroku linux),但是我移动了文件,做了一个git commit,然后再次移动并重新提交,以确保 c> Company.js 在git存储库中大写。这并没有解决问题,我不知道这个问题可能是什么。

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.

推荐答案

问题是由于case灵敏度和文件命名。 Mac OS X不区分大小写(但知道),而Heroku基于Linux并区分大小写。通过从终端运行 heroku run bash ,我可以看到Heroku的文件系统上如何显示 / models 。解决方案是在本地系统上将 User.js Company.js 重命名为新的临时文件,提交更改git,然后重新命名为 User.js Company.js 注意大写的第一个字母,然后提交通过git再次更改。以前我曾尝试将文件直接从 user.js 更改为 User.js company.js Company.js ,但git commit和区分大小写的文件名更改并未反映在Heroku上。

The problem was due to case sensitivity and file nameing. 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 + Node:找不到模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆