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

Express中使用express-handlebars报错:TypeError: exphbs is not a function

解决Express中注册handlebars模板引擎的TypeError: exphbs is not a function错误

报错原因

express-handlebars从v4版本开始,导出方式发生了变化——不再直接暴露可调用的函数,而是导出包含create()方法的对象,所以直接调用exphbs()会触发类型错误。

解决方案

方案1:使用新版本的正确调用方式

修改代码,调用exphbs.create()来创建引擎实例:

const express = require('express')
const { create } = require("express-handlebars");
const path = require("path");
const app = express()
const port = 3000

// 使用create方法生成引擎实例
app.engine("handlebars", create().engine);
app.set("view engine", "handlebars");

也可以保留原变量名,直接调用exphbs.create():

const exphbs = require("express-handlebars");
// ...其他代码
app.engine("handlebars", exphbs.create().engine);

方案2:降级到旧版本(仅适用于有兼容需求的场景)

如果不想修改代码,可以降级到v3.x版本,执行以下命令:

npm uninstall express-handlebars
npm install express-handlebars@3.1.0

降级后原代码即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 02:15:03