Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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. Below you can find two different versions of the app.js code that I am trying to run:

VERSION 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

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:

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)

<!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. I would really appreciate it if someone could point me in the right direction or, even better, explain why this is happening.

like image 685
vladzam Avatar asked Aug 07 '14 11:08

vladzam


2 Answers

From the consolidate documentation:

NOTE: you must still install the engines you wish to use, add them to your package.json dependencies.

Run the following and try again.

npm install --save handlebars
like image 72
Ben Fortune Avatar answered Sep 19 '22 02:09

Ben Fortune


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.

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:

import handlebars from 'handlebars'.

(The above applies to the new ES6 modules and the older require() format.)

like image 24
Git Paul Avatar answered Sep 21 '22 02:09

Git Paul