Tuesday, September 4, 2012

Knockout RequireJs / AMD module name convention inconsistency

When using various RequireJS/ AMD with various knockout-related scripts if there is an inconsistency in the named knockout module it will throw a RequireJS module load error.

While Knockout itself isn't exporting itself with a specific name, various scripts that depend on Knockout look for a named module (or at least a relative module. In my case, I'm using both knockout.mapping and koExternalTemplateEngine-amd

In knockout.mapping:

else if (typeof define === "function" && define["amd"]) { define(["knockout", "exports"], factory);

In koExternalTemplateEngine-amd.js

define(['ko','jquery', 'infuser'], function(ko, jQuery, infuser){

If you have abstracted away the knockout version numbers in a config paths setting:

requirejs.config({ paths: { ko: 'knockout-2.1.0.debug' } });

Knockout.mapping will fail to find the named 'knockout' module. To fix this you need to change the module name to be consistent with your path alias (you could change the alias to 'knockout' and update koExternalTemplate-amd as well of course). More importantly you need to remember that you changed it so next time you update knockout-mapping you don't break your code and spend an hour trying to figure out why! :-)

else if (typeof define === "function" && define["amd"]) { define(["ko", "exports"], factory);

No comments:

Post a Comment