Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Express 4.0:找不到模块';html'&;找不到模块';把手';_Node.js_Express - Fatal编程技术网

Node.js Express 4.0:找不到模块';html'&;找不到模块';把手';

Node.js Express 4.0:找不到模块';html'&;找不到模块';把手';,node.js,express,Node.js,Express,在过去一个小时左右的时间里,我一直在试图弄清楚为什么在启动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

在过去一个小时左右的时间里,我一直在试图弄清楚为什么在启动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文件,因为我正在试验均值堆栈。在下面,您可以找到我正在尝试运行的
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 + '...');
运行代码的版本2时,我得到以下eror:

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)


测试应用
你好,世界!

我已经研究了有关StackOverflow这个主题的其他问题,但没有找到合适的解决方案。如果有人能为我指出正确的方向,或者更好地解释为什么会发生这种情况,我将不胜感激。

来自合并文档:

注意:您仍然必须安装希望使用的引擎,将它们添加到您的package.json依赖项中

运行以下命令并重试

npm install --save handlebars

有时,如果在导入模块时使用了错误的案例,则可能会发生此错误

例如:

import Handlebars from 'Handlebars'
将在MacOS上工作,因为它有一个不区分大小写的文件系统

但是在Linux机器上(带有区分大小写的文件系统),您需要匹配Handlebar模块中定义的精确值,如下所示:

import handlebars from 'handlebars'.

(以上适用于新的ES6模块和旧的require()格式。)

适用于哪个版本?1或2?这对我来说很奇怪,因为我在package.json的依赖项中也有Handlebar 2.0。
import handlebars from 'handlebars'.