Making Metalsmith to work with Nunjucks

Slava Fomin II
1 min readNov 18, 2017

--

I’ve spent at least an hour today desperately trying to make Metalsmith to work with Nunjucks. Due to the lack of documentation it was a tiresome process. So I’ve decided to do a quick article on this topic.

The final solution, actually looks very simple, but it took me some time to sip through the various third-party code to figure out how to configure the integration.

Well, here it goes, the minimal working example:

I’m using metalsmith-in-place plugin as a starting point. It would actually use jstransformer under the hood to process the files passed to it. Also, don’t forget to install jstransformer-nunjucks dependency to integrate jstransformers framework with nunjucks.

It is also very important to use the correct filename extension with your source files. I was using .nj extension, which I lately found out was not supported by the jstransformer-nunjucks. The correct extension is: .njk as in example above.

Additionally, you will need to pass the path option to jstransformer-nunjucks in order for extends and include nunjucks expressions to work correctly, otherwise it will not be able to resolve the paths properly.

Good luck with your project, I hope this article saved you a bit of time!

--

--