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

部署Express应用至Heroku时无法查找index视图的问题求助

Express部署Heroku时无法查找index视图的解决方案

将Express应用部署到Heroku时,始终出现Failed to lookup view 'index' in views directory '/app/views'错误,但本地localhost:3000运行完全正常。以下是相关信息及解决方案:

Heroku运行日志

2022-11-13T14:46:11.815962+00:00 heroku[web.1]: Starting process with command `node server.js`
2022-11-13T14:46:15.619107+00:00 app[web.1]: Server has started
2022-11-13T14:46:15.861854+00:00 app[web.1]: DB connected successfully
2022-11-13T14:46:16.018081+00:00 heroku[web.1]: State changed from starting to up
2022-11-13T14:52:47.472985+00:00 heroku[router]: at=info method=GET path="/" host=stark-lowlands-39210.herokuapp.com request_id=ef33e137-00ea-48d8-be4f-05b0c4fa505a fwd="12.205.212.138" dyno=web.1 connect=0ms service=27ms status=500 bytes=404 protocol=https
2022-11-13T14:52:47.477306+00:00 app[web.1]: Error: Failed to lookup view "index" in views directory "/app/views"
2022-11-13T14:52:47.477309+00:00 app[web.1]: at Function.render (/app/node_modules/express/lib/application.js:597:17)
2022-11-13T14:52:47.477309+00:00 app[web.1]: at ServerResponse.render (/app/node_modules/express/lib/response.js:1039:7)
2022-11-13T14:52:47.477311+00:00 app[web.1]: at /app/app.js:20:9
2022-11-13T14:52:47.477312+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2022-11-13T14:52:47.477312+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:144:13)
2022-11-13T14:52:47.477313+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:114:3)
2022-11-13T14:52:47.477313+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2022-11-13T14:52:47.477314+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:284:15
2022-11-13T14:52:47.477315+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:346:12)
2022-11-13T14:52:47.477315+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:280:10)

文件夹结构

项目根目录包含:

  • app.js
  • server.js
  • views文件夹(内含index.ejs)
  • public文件夹
  • model文件夹(内含fileSchema.js)
  • node_modules文件夹
  • package.json等配置文件

app.js代码

const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const mongoose  = require("mongoose");
const https = require("https");
// const File = require("./model/fileSchema");
const multer = require("multer");
const http = require('http');

const app = express();

// Set up code
app.set("view engine", "ejs");
app.set("views", path.join(__dirname + "/views"));
app.use(express.static(`${__dirname}/public`));
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.static("public"));

app.get('/', function (req, res) {
    res.render("index");
});

server.js代码

const app = require("./app");
const mongoose = require("mongoose");
const http = require('http');
const https = require("https");
const path = require("path");

process.on("uncaughtException", (err) => {
  console.log("UNCAUGHT EXCEPTION, APP SHUTTING NOW!!");
  console.log(err.message, err.name);
  process.exit(1);
});

const DB = "mongodb+srv://boepartners:missyangus123@cluster0.dm8gvgf.mongodb.net/BOE";

mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    autoIndex: true,
  })
  .then(() => {
    console.log("DB connected successfully");
  });

  let port = process.env.PORT;
  if (port == null || port == "") {
    port = 3000;
  }

  app.listen(port, function(){
    console.log("Server has started");
  });

解决方案

1. 修复views路径配置

当前path.join(__dirname + "/views")的写法存在兼容问题,path.join本身会自动处理系统路径分隔符,无需手动拼接。修改为:

app.set("views", path.join(__dirname, "views"));

该写法可兼容Windows和Unix(Heroku使用Unix环境),避免路径拼接错误。

2. 确认views文件已提交到Git

Heroku仅部署Git仓库中包含的文件,需检查:

  • 查看.gitignore文件,确保未忽略views文件夹或.ejs文件
  • 执行git add views/和git commit -m "提交views文件夹",重新部署到Heroku

3. 验证Heroku服务器上的文件结构

通过Heroku CLI查看服务器文件结构,确认views目录及index.ejs存在:

heroku run bash -a stark-lowlands-39210
ls -la /app/views

若文件不存在,说明未正确提交到Git,需重新提交后部署。

4. 检查EJS依赖配置

确保ejs被添加到package.json的dependencies而非devDependencies(Heroku默认不安装开发依赖):

"dependencies": {
  "ejs": "^3.1.8",
  // 其他依赖...
}

若之前在devDependencies中,执行npm install ejs --save,提交package.json后重新部署。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:46:16