You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Node.js后端至Heroku遇模块找不到错误求助

Fixing "Cannot find module './config/keys'" on Heroku

Hey there, let’s break down why your Node.js app is hitting this error and how to fix it quickly.

The Root Cause

From the heroku run 'ls -al' output you shared, it’s clear the ./config/keys file doesn’t exist on your Heroku dyno. The most common reason for this is that the file is listed in your .gitignore, so Git isn’t tracking it—and since Heroku deploys directly from your Git repo, the file never makes it to the server.

Step-by-Step Fixes

1. Confirm the file is ignored

First, check your local .gitignore file. Open it up and look for lines like:

config/keys.js
# Or even the entire config directory:
config/

If you see either, that’s exactly why the file is missing on Heroku.

2. Choose the right solution based on sensitive data

How you fix this depends on whether keys.js contains sensitive info (like API secrets, database credentials, etc.):

Option A: If the file has sensitive data (RECOMMENDED)

Never commit sensitive keys to Git! Instead, use Heroku’s environment variables:

  • Create a config/keys.js.example file locally with placeholder values (so other developers know what keys are needed) and commit this to Git:
    // config/keys.js.example
    module.exports = {
      dbURI: "your_database_uri_here",
      apiKey: "your_api_key_here"
    };
    
  • Set the actual values as environment variables on Heroku. You can do this via the Heroku Dashboard (under your app’s Settings > Config Vars) or using the CLI:
    heroku config:set DB_URI=your_real_database_uri
    heroku config:set API_KEY=your_real_api_key
    
  • Update your local keys.js (not committed) to pull values from the environment:
    // config/keys.js (local only)
    module.exports = {
      dbURI: process.env.DB_URI,
      apiKey: process.env.API_KEY
    };
    
  • Push your changes to Heroku:
    git add config/keys.js.example
    git commit -m "Add keys example file, switch to env vars"
    git push heroku main
    
Option B: If the file has no sensitive data

If the file is safe to commit, remove it from .gitignore, then push it to Heroku:

  • Edit .gitignore to delete the line that excludes config/keys.js or the config/ directory.
  • Commit the file to Git:
    git add config/keys.js
    git commit -m "Add config/keys.js to repo"
    git push heroku main
    

3. Verify the fix

After pushing, run heroku run 'ls -al config/' to confirm the keys.js file now exists on the dyno. Then restart your app with heroku restart and test it in the browser.

内容的提问来源于stack exchange,提问作者user8359832

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:36:39