#755·nunjucks

Relative paths not working in precompiled templates in the browser

Author: rhenglesCreated May 12, 2016Updated Oct 6, 2025
Labelsbug

I am trying to use the PrecompiledLoader in the browser, but I'm getting this error:

TypeError: path.dirname is not a function at Obj.extend.resolve (http://localhost:8576/js/nunjucks-slim.js:2504:35) at Obj.extend.resolveTemplate (http://localhost:8576/js/nunjucks-slim.js:597:56) at Obj.extend.getTemplate (http://localhost:8576/js/nunjucks-slim.js:627:35) at root as rootRenderFunc at Obj.extend.render (http://localhost:8576/js/nunjucks-slim.js:923:16) at http://localhost:8576/js/nunjucks-slim.js:756:36 at createTemplate (http://localhost:8576/js/nunjucks-slim.js:679:26) at handle (http://localhost:8576/js/nunjucks-slim.js:694:26) at http://localhost:8576/js/nunjucks-slim.js:708:22 at next (http://localhost:8576/js/nunjucks-slim.js:347:14)

I found out it's because the precompiled templates still have the relative paths - they're not converted to absolute paths - and the nunjucks runtime is calling Loader#resolve() which uses "path.dirname" and "path.resolve" from require("path").

In my case, I don't know how, but the "path" variable has this value:

{
    "PrecompiledLoader": function () {
            if(prototype.init) {
                prototype.init.apply(this, arguments);
            }
    }
}

How do I solve this? I'm using nunjucks 2.4.2. I did a little research here, and if I'm not mistaken, this was supposed to have been implemented in #349.

Thanks!

@carljm