Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs: Error: Cannot find module 'html'

Tags:

im using nodejs and im trying to serve only html files (no jade, ejs ... engines).

heres my entry point (index.js) code:

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'));

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

app.listen(app.get('port'), function() {
});

This is doing just fine when i hit the url "localhost:5000/", but when i try something like "localhost:5000/whatever" i got the following message: Error: Cannot find module 'html'

im new to nodejs, but i want all routes to render the index.html file. How can i do that ???

Thank you.

like image 448
dafriskymonkey Avatar asked Nov 06 '14 12:11

dafriskymonkey


People also ask

How do I resolve Cannot find module error using node JS?

To fix the Cannot find module error, simply install the missing modules using npm . This will install the project's dependencies into your project so that you can use them. Sometimes, this might still not resolve it for you. In this case, you'll want to just delete your node_modules folder and lock file ( package-lock.

Can not find module HTML?

To solve the error "Cannot find module 'html-webpack-plugin'", make sure to install the html-webpack-plugin package by opening your terminal in your project's root directory and running the following command: npm i -D html-webpack-plugin and restart your IDE and dev server.

What does Cannot find module mean?

The "Cannot find module" error in Node. js occurs for multiple reasons: Forgetting to install a third-party package with npm i somePackage . Pointing the node command to a file that doesn't exist. Having an outdated version of the package, or an IDE or development server glitch.


1 Answers

You need to specify your view folder and parse the engine to HTML.

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());



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

app.listen(app.get('port'), function() {
});
like image 135
vmontanheiro Avatar answered Oct 02 '22 16:10

vmontanheiro