简体   繁体   English

Heroku + Node:找不到模块错误

[英]Heroku + Node: Cannot find module error

My Node app is running fine locally, but has run into an error when deploying to Heroku.我的 Node 应用程序在本地运行良好,但在部署到 Heroku 时遇到了错误。 The app uses Sequelize in a /models folder, which contains index.js , Company.js and Users.js .该应用程序在/models文件夹中使用 Sequelize,其中包含index.jsCompany.jsUsers.js Locally, I am able to import the models using the following code in /models/index.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.js is capitalized in the git repository.最初我认为这是由于区分大小写(本地 mac 与 heroku linux),但我移动了文件,进行了 git 提交,然后移回并再次提交以确保Company.js在 git 存储库中大写。 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. Mac OS X 不区分大小写(但知道),而 Heroku 基于 Linux 并且区分大小写。 By running heroku run bash from my terminal, I was able to see how the /models folder appeared on Heroku's file system.通过从我的终端运行heroku run bash ,我能够看到/models文件夹如何出现在 Heroku 的文件系统上。 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.解决方案是将本地系统上的User.jsCompany.js重命名为新的临时文件,将更改提交到 git,然后重命名回User.jsCompany.js ,注意首字母大写,然后提交更改再次通过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.以前我曾尝试将文件直接从user.js重命名为User.js ,将company.js重命名为Company.js但 git commit 和区分大小写的文件名更改并未反映在 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.我遇到过这样的错误,原因是我将模块module.js重命名为Module.js并且 Heroku 缓存与名称冲突。 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来源: 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.在 git repo 上,它被大写。

'../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.我不得不使用git mv在本地重命名文件,然后重新上传它以解决问题。

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 .不确定这是否与此处描述的问题相同,但对我而言,我的require("dotenv").config()不是以代码运行的环境为条件的,因此 Heroku 无法找到它,因为它安装为一个devDependency

Fix:修复:

if (process.env.NODE_ENV !== "production") {
  require("dotenv").config();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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