This might sound like duplicate of this and few others but the solutions given in those stack were not helpful.
I am creating an HTML using EJS templating, but while including a partial I am receiving following error :
throw new Error('Could not find include include file.');
Below is the project structure
|
|
|-----index.js
|-----view
| |
| |----pages
| | |----mainTemplate.ejs
| |----partials
| | |----bodyTemplate.ejs
| |
Now as per doc the include takes relative path so code in my mainTemplate.ejs includes the bodyTemplate like this
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample</title>
</head>
<body>
<!-- Insert Body here -->
<div class="container">
<div class="starter-template">
<% include ../partials/bodyTemplate.ejs%>
</div>
</div>
</body>
</html>
While code in bodyTemlate.ejs is
<% console.log("#########################");%>
<h1>Hi there !! </h1>
I am receiving Could not find include include file This error is not much specific as to which path is being accessed here.
I have tried removing .. from the path still no use? I have same project structure in my EJS application and this same code snippet works just fine? Is this something specific to express causing such issue?
EDIT
This is index.js
var fs = require('fs'),
ejs = require("ejs");
function ejs2html(path, information) {
fs.readFile(path, 'utf8', function(err, data) {
if (err) {
console.log("ERROR: " + err);
return false;
}
var ejs_string = data,
template = ejs.compile(ejs_string),
html = template(information);
fs.writeFile(path + '.html', html, function(err) {
if (err) {
console.log(err);
return false
}
return true;
});
});
}
// console.log("This is path --> ", __dirname + '/view/pages/bodyTemplate.ejs');
ejs2html(__dirname + '/view/pages/mainTemplate.ejs');