简体   繁体   English

Express 4.0:找不到模块'html'&找不到模块'把手'

[英]Express 4.0: Cannot find module 'html' & Cannot find module 'handlebars'

For the past hour or so I've been trying to figure out why I keep getting the following error when starting an Express 4.0 application: 在过去一小时左右的时间里,我一直试图弄清楚为什么在启动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)

Basically, I just want to use plain html files for my views, as I am experimenting with the MEAN stack. 基本上,我只想在我的视图中使用普通的html文件,因为我正在尝试使用MEAN堆栈。 Below you can find two different versions of the app.js code that I am trying to run: 您可以在下面找到我尝试运行的两个不同版本的app.js代码:

VERSION 1 版本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 + '...');

VERSION 2 版本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 + '...');

When running VERSION 2 of the code, I get the following eror: 运行代码的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)

VIEW (INDEX.HTML) 查看(INDEX.HTML)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Application</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

I have already looked at the other questions on this topic on StackOverflow, but could not find a proper solution. 我已经在StackOverflow上查看了有关此主题的其他问题,但找不到合适的解决方案。 I would really appreciate it if someone could point me in the right direction or, even better, explain why this is happening. 如果有人能指出我正确的方向,或者甚至更好地解释为什么会这样,我真的很感激。

From the consolidate documentation: 从合并文档:

NOTE: you must still install the engines you wish to use, add them to your package.json dependencies. 注意:您仍然必须安装要使用的引擎,将它们添加到package.json依赖项中。

Run the following and try again. 运行以下命令,然后重试。

npm install --save handlebars

Sometimes this error can occur if the wrong case is used when importing the module. 如果在导入模块时使用了错误的情况,有时会发生此错误。

For example: 例如:

import Handlebars from 'Handlebars'

will work on MacOS, as it has a case insensitive filesystem. 将适用于MacOS,因为它有一个不区分大小写的文件系统。

But on a Linux machine (with a case sensitive filesystem), you'll need to match the exact defined in the Handlebars module, which would look like: 但是在Linux机器上(带有区分大小写的文件系统),您需要匹配Handlebars模块中的确切定义,如下所示:

import handlebars from 'handlebars'.

(The above applies to the new ES6 modules and the older require() format.) (以上内容适用于新的ES6模块和较旧的require()格式。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM