require.config({
paths: {
jquery: 'libs/jquery',
underscore: 'libs/underscore',
}
});
define([
'jquery',
'underscore',
], function($,_){
var test = _.template("hello: <%= name %>",{name:"warren"});
});
Given the above code using requirejs to load jquery and underscore, why am I getting this error…
Uncaught TypeError: Cannot call method 'template' of null
All JS seems to be loading… The error points to the _.template line. This is driving me crazy…
Are you sure you’ve bundled the underscore library to be usable with requireJS ?
When you write :
The ‘_’ variable (second argument of the callback function) is assigned with the export of the underscore library (second dependency specified).
I guess you used the original version of underscore, which is not package for requireJS.
In your underscore.js file, add the following line at the begining:
and this line at the end:
Thus, the export are well configured.
Another possible solution is to use the global ‘_’ variable: all you need to do is to remove the second argument of your callback.
From there, your browser will use the ‘_’ global variable, which is defined in underscore, and which is set when the callback is called.
Same behaviour is expected with jQuery.