Search code examples
javascripthandlebars.js

Handlebars throwing error: "Uncaught TypeError: Cannot set property 'Handlebars' of undefined"


I have npm installed handlebars v4.05:

image

I use grunt to compile a "searchBundle.js", it (grunt) bundles jquery, underscore, handlebars and all of my other files in my search directory into the searchBundle.js file. (Not sure how relevant that is, but thought i'd mention, just incase).

When I load the page though, it throws this error: "Uncaught TypeError: Cannot set property 'Handlebars' of undefined"

I tried looking for a solution but only found an issue submitted for the https://github.com/brunch/handlebars-brunch/ package. It basically said that babel was the issue (which I use during compiling the searchBundle.js file via grunt-babel) and so I tried the solution that it proposed in the issue (see here: https://github.com/brunch/handlebars-brunch/issues/51).

However this didnt fix the issue. Here is a screenshot of the line that the console is flagging up:

image

Any ideas? I am not seeing this flagged as an issue on the handlebars repo and I amn't seeming to find anything on here or google on what could be causing this.


Solution

  • This happened to me while running in strict mode (you can't use 'this' to reference a window, which is what the first argument of the function should be).

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

    Fix last line:

    })(this || window, function() {