Trying to render a nunjucks template but getting Error: template not found: email.html
.
server/
views/
email/
email.html
workers/
email.worker.js
//email.worker.js
function createMessage(articles) {
console.log(__dirname) // /<path>/server/workers
nunjucks.configure('../views/email/');
return nunjucks.render('email.html', articles);
}
No idea what's wrong here.
I had same issue. I found this at the documentation:
If you run the node server at the root directory, the template path would be
server/views
.In my case, the server script is in
public
directory.So, when i run the server from the root directory, the nunjucks configuration will look like this:
It works.
But if I run the server from the
public
directory, the tempate will not found.Had the same issue, try this if it helps. If you're using express and you have a
views
folder:From
nunjucks
docsYou can use nodejs'
__dirname
to resolve the path for you asI had the same issue my solution was using path module: