Hi. I’m new when it comes to Browserify (and modules as a whole in JS) and am wanting help on how to export/import them corretly. I write my JS using The Revealing Module Pattern in an object, however I keep getting errors when compiling it using the way I’m importing/exporting in Browserify.
Below is a basic example of how I’m writing my module, using the namespace of App.
var App = App || {};
App.addBodyClass = (function ($) {
'use strict';
var init = function() {
$('body').addClass('test');
};
return {
init: init
};
})(jQuery);
The way how I’ve been trying to do it, is in /modules/addBodyClass.js I have…
require('jQuery');
'use strict';
var App = App || {};
App.addBodyClass = (function ($) {
var init = function() {
$('body').addClass('test');
};
return {
init: init
};
})(jQuery);
module.exports = APP.addBodyClass;
Then I’m importing into my index.js file the following way
var addBodyClass = require('./modules/addBodyClass');
var App = App || {};
$(function () {
'use strict';
App.addBodyClass.init();
});
Can anyone help/explain how I can get this working, and if there’s any improvements that can be made? Improvements such as, do I use “use strict” inside each module, or in index.js? Do I need to use var App = App || {}; inside each module as it’s the namespace, or just once at the top of the index.js file?