Search code examples
javascriptnpmhandlebars.jstemplate-engine

Error when precompiling all handlebars templates files in one js file


Here's my folder structure:

app
└───templates
    ├───templ1.hbs
    ├───templ2.hbs
    └───templ3.hbs

I want to compile (precompile) all templN.hbs handlebars template files in one templ.js file, but when i try to do it via console

$ handlebars *.hbs -f templ.js

compiling fails with this error

(...)\AppData\Roaming\npm\node_modules\handlebars\bin\handlebars:120
    throw err;
    ^
 Error: Unable to open template file "*.hbs"
    at (...)\AppData\Roaming\npm\node_modules\handlebars\dist\cjs\precompiler.js:107:25
    at FSReqWrap.oncomplete (fs.js:82:15)

What's the matter?


Solution

  • After a bit of practice with Handlebars and its precompiler i found out the solution was as simple as stupid my question was.

    First of all it's better to stay in the root folder, hence in my case stay in app folder. Then, to compile all .hbs templates, simply pass the entire ./handlebars folder as input to handlebars precompiler and specify the extension of templates he should look for:

    $ handlebars ./templates -f templ.js --extension "hbs"
    

    Everything should work fine.

    For more precompiler's options see http://handlebarsjs.com/precompilation.html