[英]Express 4.0: Cannot find module 'html' & Cannot find module 'handlebars'
在過去一小時左右的時間里,我一直試圖弄清楚為什么在啟動Express 4.0應用程序時出現以下錯誤:
Error: Cannot find module 'html'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at new View (e:\Multivision\node_modules\express\lib\view.js:43:49)
at Function.app.render (e:\Multivision\node_modules\express\lib\application.
js:499:12)
at ServerResponse.res.render (e:\Multivision\node_modules\express\lib\respon
se.js:955:7)
at e:\Multivision\server.js:11:6
at Layer.handle [as handle_request] (e:\Multivision\node_modules\express\lib
\router\layer.js:76:5)
at next (e:\Multivision\node_modules\express\lib\router\route.js:100:13)
基本上,我只想在我的視圖中使用普通的html文件,因為我正在嘗試使用MEAN堆棧。 您可以在下面找到我嘗試運行的兩個不同版本的app.js
代碼:
版本1
var express = require('express'),
app = express();
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
port = 3000;
app.set('views', __dirname + '/server/views');
app.set('view engine', 'html');
app.get('*', function(req, res) {
res.render('index');
});
app.listen(port);
console.log('Listening on port ' + port + '...');
版本2
var express = require('express'),
app = express();
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
port = 3000;
app.set('views', __dirname + '/server/views');
app.engine('html', require('consolidate').handlebars);
app.set('view engine', 'html');
app.get('*', function(req, res) {
res.render('index');
});
app.listen(port);
console.log('Listening on port ' + port + '...');
運行代碼的VERSION 2時,我得到以下錯誤:
Error: Cannot find module 'handlebars'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Function.exports.handlebars.render (e:\Multivision\node_modules\consolida
te\lib\consolidate.js:488:62)
at e:\Multivision\node_modules\consolidate\lib\consolidate.js:146:25
at e:\Multivision\node_modules\consolidate\lib\consolidate.js:99:5
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
查看(INDEX.HTML)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test Application</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
我已經在StackOverflow上查看了有關此主題的其他問題,但找不到合適的解決方案。 如果有人能指出我正確的方向,或者甚至更好地解釋為什么會這樣,我真的很感激。
從合並文檔:
注意:您仍然必須安裝要使用的引擎,將它們添加到package.json依賴項中。
運行以下命令,然后重試。
npm install --save handlebars
如果在導入模塊時使用了錯誤的情況,有時會發生此錯誤。
例如:
import Handlebars from 'Handlebars'
將適用於MacOS,因為它有一個不區分大小寫的文件系統。
但是在Linux機器上(帶有區分大小寫的文件系統),您需要匹配Handlebars模塊中的確切定義,如下所示:
import handlebars from 'handlebars'.
(以上內容適用於新的ES6模塊和較舊的require()格式。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.