我尝试使用express-handlebar视图引擎进行express,但一直收到以下错误:
Error: Cannot find module 'hbs'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at new View (C:\Users\WilPrim\Desktop\node-app\node_modules\express\lib\view.js:81:14)
at Function.render (C:\Users\WilPrim\Desktop\node-app\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\Users\WilPrim\Desktop\node-app\node_modules\express\lib\response.js:1008:7)
at C:\Users\WilPrim\Desktop\node-app\routes\routes.js:5:6
at Layer.handle [as handle_request] (C:\Users\WilPrim\Desktop\node-app\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\WilPrim\Desktop\node-app\node_modules\express\lib\router\route.js:137:13)
这是我的代码:
const express = require("express");
const bodyParser = require("body-parser");
const hbs = require("express-handlebars");
const router = require("./routes/routes");
//set up express app
var app = express();
app.use(bodyParser.json());
app.set('view engine', 'hbs');
app.set('views', './views');
app.set('view options', {layout: './layouts/layout'});
app.use(router);
6条答案
按热度按时间qzlgjiam1#
根据
express-handlebars
documentation,您必须手动设置和注册视图引擎:在文档中再往下看一点,看起来您仍然可以使用
.hbs
扩展,但需要进行一些配置:fumotvh32#
使用命令--
sudo npm link hbs
a0x5cqrl3#
您可能已将主文件夹命名为“express”,只需更改文件夹名称,删除“package.json”并再次尝试“npm install”,然后尝试“npm install express”“npm install hbs”
lf3rwulv4#
neekobn85#
在终端上安装此模块--〉npm i hbs
luaexgnf6#
尝试npm安装和npm审计修复多次然后在ex后运行服务器