After finally publishing my first package on NPM, I encountered some frustrating issues that I just can't seem to solve. I have a feeling that the solution is right in front of me, but I need to ask for help before I lose my mind.
The problem arises when I try to bundle the files using browserify. I have encapsulated my micro-lib in an IIFE and I am attempting to module.export the constructor at the end of the IIFE. Here is a snippet of what it looks like:
var Package = (function() {
/* Constructor */
function Package(foo, bar) {
this.foo = foo;
this.bar = bar;
}
// Several private methods (not necessarily private)
// API methods I want users to access from the browser console
Package.prototype.$method = function() {
return this.foo;
};
return Package;
})();
// Expose constructor?
module.exports = Package;
My aim is to enable users to require my package like this:
var package = require('package-name');
var P = new package(foo, bar);
So far, the above code works fine. I can log `P` to the console without any issues. However, when I want users to access the prototype method like this:
P.$config();
It doesn't work as expected. The user successfully instantiates the object from the package, but when they try to access the prototype method, it returns `undefined`. It seems that after bundling the file with browserify and wrapping it in an IIFE, the package is no longer accessible from the browser console. What am I missing here? I know there are probably several things, but I can't seem to figure it out.
I've attempted bundling with browserify using `--standalone` to expose the `module.exports` package variable, but it's not solving the issue. Can someone please take a moment to explain the workflow for publishing an npm (front end) package? Do I need to remove the IIFE around my package and have the constructor exposed openly?
Thank you in advance for your help. I really appreciate it.