Nunjucks: Precompile issue

Created on 1 Feb 2017  路  9Comments  路  Source: mozilla/nunjucks

Following the doc here: https://mozilla.github.io/nunjucks/api.html#precompiling

I try to precompile a full directory with this method:

nunjucks.precompile('/dir/to/views', { env: env });

but it does not works, it just returns an empty string.

Please, note that it also happens with the cli command nunjuck-precompile.

Additionally adding a wildcard will trigger an error:

nunjucks.precompile('/dir/to/views/*', { env: env });
node_modules/nunjucks/src/precompile.js:78
    else if(pathStats.isFile()) {
                      ^
TypeError: pathStats.isFile is not a function
at Object.precompile (node_modules/nunjucks/src/precompile.js:78:23)
at Object.<anonymous> (scripts/precompile.js:3:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3

Most helpful comment

I have the same issue on ubuntu with the cli version.
nunjucks-precompile <dir> > templates.js ressults in a file with an empty line.
While precompiling one and one works fine.

Could be the fact that I use .njk but have not tried to change yet. Even so, the documentation mentions nothing about file types.

All 9 comments

I have same issue, could you fix it?

@ljmasague no i couldn't fix it and I decided to dont use nunjuck for a few reasons including this one

ok... thanks for the info :)

In my case, I got this error when attempting to use a path that was relative to the path configured in my nunjucks environment.

This did not work:

nunjucks.configure('./path/to/templates')
const template = nunjucks.precompile('./template-name.html', { name: 'template' })

But this did:

nunjucks.configure('./path/to/templates')
const template = nunjucks.precompile(
  './path/to/templates/template-name.html',
  { name: 'template' }
)

Hope that helps.

Judging by the thumbs-up emoji I'm assuming that answer resolved the issue.

I thumbed up because I liked the fact he shared the fix. Didn't actually try by myself

This is still an issue. Attempting to precompile an entire directory per the documentation fails with the pathStats.isFile error, even with an absolute path.

I have the same issue on ubuntu with the cli version.
nunjucks-precompile <dir> > templates.js ressults in a file with an empty line.
While precompiling one and one works fine.

Could be the fact that I use .njk but have not tried to change yet. Even so, the documentation mentions nothing about file types.

I've hit this for the second time and this time I did some digging in the code and found a hint:
.default('include', ['\\.html$', '\\.jinja$'])

I changed my files to .jinja and voila...

Now: There's speak of the community adapting .njk in the documentation which I think is the source of the confusion. I will find some time shortly and add .njk to the precompiler.

Could this be your problem as well ?

Was this page helpful?
0 / 5 - 0 ratings