Handlebars exphbs 不是一个函数

huangapple go评论51阅读模式
英文:

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(&#39;express&#39;)
const exphbs = require(&quot;express-handlebars&quot;);
const path = require(&quot;path&quot;);
const app = express()
const port = 3000

app.engine(&quot;handlebars&quot;, exphbs());
app.set(&quot;view engine&quot;, &quot;handlebars&quot;);

答案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(&quot;handlebars&quot;, exphbs.engine());

Alternatively, you can destructure the object and take out engine directly:

const express = require(&#39;express&#39;)
const { engine } = require(&quot;express-handlebars&quot;);
const path = require(&quot;path&quot;);
const app = express()
const port = 3000

app.engine(&quot;handlebars&quot;, engine());
app.set(&quot;view engine&quot;, &quot;handlebars&quot;);

答案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(&#39;express-handlebars&#39;);

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(&#39;express&#39;)
  const exphbs = require(&quot;express-handlebars&quot;);
  const path = require(&quot;path&quot;);
  const app = express()
  const port = 3000

  app.engine(&quot;handlebars&quot;, exphbs.engine());
  app.set(&quot;view engine&quot;, &quot;handlebars&quot;);

huangapple
  • 本文由 发表于 2023年5月14日 10:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定