Heroku + Node: Cannot find module error

后端 未结 6 560
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 09:10

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.

相关标签:
6条回答
  • 2020-12-13 09:26

    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.

    0 讨论(0)
  • 2020-12-13 09:27

    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.

    0 讨论(0)
  • 2020-12-13 09:35

    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();
    }
    
    0 讨论(0)
  • 2020-12-13 09:36

    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

    0 讨论(0)
  • 2020-12-13 09:38

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

    0 讨论(0)
  • 2020-12-13 09:39

    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.

    0 讨论(0)
提交回复
热议问题