node.js Heroku + Node:找不到模块错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19341975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Heroku + Node: Cannot find module error
提问by surfearth
My Node app is running fine locally, but has run into an error when deploying to Heroku. The app uses Sequelize in a /modelsfolder, which contains index.js, Company.jsand Users.js. Locally, I am able to import the models using the following code in /models/index.js:
我的 Node 应用程序在本地运行良好,但在部署到 Heroku 时遇到了错误。该应用程序在一个/models文件夹中使用 Sequelize ,其中包含index.js、Company.js和Users.js。在本地,我可以使用以下代码导入模型/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:
这工作正常,但是,当我部署到 Heroku 时,应用程序崩溃并显示以下错误:
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.jsis capitalized in the git repository. This didn't solve the problem and I'm not sure what the issue could be.
最初我认为这是由于区分大小写(本地 mac 与 heroku linux),但我移动了文件,进行了 git commit,然后移回并再次提交以确保Company.js在 git 存储库中大写。这并没有解决问题,我不确定问题可能是什么。
回答by surfearth
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 bashfrom my terminal, I was able to see how the /modelsfolder appeared on Heroku's file system. The solution was to rename User.jsand Company.json my local system to new temporary files, commit the changes to git, then rename back to User.jsand Company.jsbeing 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.jsto User.jsand company.jsto Company.jsbut the git commit and case-sensitive file name changes did not reflect on Heroku.
问题是由于区分大小写和文件命名。Mac OS X 不区分大小写(但知道),而 Heroku 基于 Linux 并且区分大小写。通过heroku run bash从我的终端运行,我能够看到/models文件夹在 Heroku 的文件系统上的显示方式。解决方案是在我的本地系统上将User.js和重命名Company.js为新的临时文件,将更改提交到 git,然后重命名回User.js并Company.js注意大写的第一个字母,然后通过 git 再次提交更改。以前我曾尝试将文件直接从user.jstoUser.js和to 重命名company.js,Company.js但 git commit 和区分大小写的文件名更改并未反映在 Heroku 上。
回答by Dan Kohn
I can't see the exact fix, but you can figure it out yourself by running heroku run bashto log into a Heroku instance, then run nodeto enter a REPL, and try requiring the paths directly.
我看不到确切的修复方法,但您可以通过运行heroku run bash登录 Heroku 实例,然后运行node以输入 REPL,并尝试直接要求路径来自己解决。
回答by Adrien Joly
For me, it was caused by a folder that I had accidentally included in .gitignore!
对我来说,这是由我不小心包含在 .gitignore 中的文件夹引起的!

