user4911648
user4911648

Reputation:

Express NodeJS Cannot find module 'html'

I have a Node Server with Express. I get the cannot find module 'html' error even though my code looks like this and should be correct in my opinion:

app.set('views', path.join(__dirname, 'build/views'));
app.use(favicon(path.join(__dirname, "build/favicon.ico")));
app.use('/scripts', express.static(path.join(__dirname, 'node_modules')));
app.use(express.static(path.join(__dirname, 'build')));

app.get('/', function(req, res){
    res.render('index.html');
});

Upvotes: 2

Views: 12483

Answers (3)

user4911648
user4911648

Reputation:

If you only want to serve a static file without passing any variables from the server to the client the easiest solution would be:

res.sendFile(path.join(__dirname + '/build/views/index.html'));

Especially when you are using AngularJS for the client side. The problem with the solution above is that mustache uses {{}} as variables recognition. So does AngularJS which might causes errors!

Upvotes: 2

rsp
rsp

Reputation: 111346

It seems that you are trying to display a static index.html file but you serve it as if it was a template. Probably Express is trying to find a module for html template format, which doesn't exist.

You may try to send the index as a static file instead of with res.render which is for rendering templates.

Some time ago I wrote an example of serving static files with Express. It's available on GitHub:

See also my other answer, where I explain it in more detail.

Upvotes: 0

abdulbari
abdulbari

Reputation: 6232

You have to set engine for HTML

Include this code in your main file

var engines = require('consolidate');

app.engine('html', engines.mustache);
app.set('view engine', 'html');

Upvotes: 3

Related Questions