Custom helper with handlebars-loader

1.9k views Asked by At

I'm using this handlebars-loader for webpack 4. Now I wanted to use a custom helper, but i only get this error

ERROR in Template execution failed: TypeError: __default(...).call is not a function

ERROR in TypeError: __default(...).call is not a function

this is my webpack.config

//handlebars-loader
{
  test: /\.(hbs)$/,
  loader: "handlebars-loader",
  options: {
    helperDirs: [path.join(__dirname, './src/hbs/helpers')],
    partialDirs: [path.join(__dirname, './src/hbs/partials')]
  },
}

and this is my simple helper

const Handlebars = require('handlebars');
Handlebars.registerHelper('repeat', function(n) {
  console.log(n);
});

and how I use it

{{#repeat 10}}
    <span> repeat</span>
{{/repeat}}   

Does anyone know what I'm doing wrong?

2

There are 2 answers

0
Sideshowlol On

Thank you, Gregor! This has been frustrating me all day!

I had a simple function in a helper file, checkActive.js :

const Handlebars = require("handlebars")

Handlebars.registerHelper("checkActive", function (pageName, linkedPageName) {
  return pageName === linkedPageName ? "active" : ""
})

This would not work and gave me this error:

ERROR in Template execution failed: TypeError: __default(...).call is not a function

ERROR in   TypeError: __default(...).call is not a function

I found your solution and tried it. Lo-and-behold it worked! This is what I am now using instead:

module.exports = function (pagename, linkedPageName) {
  return pagename === linkedPageName ? "active" : ""
  console.log(pagename)
};

In order for this to work, you need to have the function saved in a file with the same name as the handlebars helper function you are calling, ie. in my case: checkActive.js

1
Gregor Voinov On

I found a solution. I have to export the helper as a module, then it works.

//repeat.js
module.exports = function(times, options) {
    console.log(times);
};