英文:
Handlebars exphbs is not a function
问题
错误是“app.engine('handlebars', exphbs()); TypeError: exphbs 不是一个函数 at Object.
英文:
Error is "app.engine("handlebars", exphbs()); 'TypeError: exphbs is not a function
at Object.<anonymous> '"
const express = require('express')
const exphbs = require("express-handlebars");
const path = require("path");
const app = express()
const port = 3000
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
答案1
得分: 2
exphbs
不是一个函数,它是由 handlebars 导出的对象(请参阅文档)。您想要使用的函数是 exphbs.engine()
,如下所示:
app.engine("handlebars", exphbs.engine());
或者,您可以解构该对象并直接取出 engine
:
const express = require('express')
const { engine } = require("express-handlebars");
const path = require("path");
const app = express()
const port = 3000
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
英文:
exphbs
isn't a function, it is an object of stuff exported by handlebars (see the documentation). The function you want to use is exphbs.engine()
like this:
app.engine("handlebars", exphbs.engine());
Alternatively, you can destructure the object and take out engine
directly:
const express = require('express')
const { engine } = require("express-handlebars");
const path = require("path");
const app = express()
const port = 3000
app.engine("handlebars", engine());
app.set("view engine", "handlebars");
答案2
得分: 1
我觉得可能是 exphbs 函数没有正确安装或导入:
npm install express-handlebars
通过以下方式导入:
const exphbs = require('express-handlebars');
如果不是这种情况,并且你已经正确安装和导入它。然后看看是否更新到最新版本解决了问题:
npm update express-handlebars
编辑:现在我想想,你需要在 exphbs 上调用 engine() 函数,以获取实际的函数,可以用它来注册 handlebars 模板引擎到你的 Express 应用程序。
正确的代码应该是:
const express = require('express')
const exphbs = require("express-handlebars");
const path = require("path");
const app = express()
const port = 3000
app.engine("handlebars", exphbs.engine());
app.set("view engine", "handlebars");
英文:
I think its possible that the exphbs function is not properly installed or imported:
npm install express-handlebars
Import it with:
const exphbs = require('express-handlebars');
If that's not the case, and you have it installed and imported correctly. Then see if updating to the latest version fixes the issue:
npm update express-handlebars
EDIT: Now that I think about it, you need to call the engine() function on exphbs to get the actual function that you can use to register the handlebars templating engine with your Express app.
The correct code should be:
const express = require('express')
const exphbs = require("express-handlebars");
const path = require("path");
const app = express()
const port = 3000
app.engine("handlebars", exphbs.engine());
app.set("view engine", "handlebars");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论