How do I structure a library for NPM (With modules)

Lets say I have a simple JS library. Most of the JS code is inside an object named after the library with the properties being the method functions. Some of these method functions require other method functions (Example, methodC() requires methodA()).


<span style="color:#323232;">const myLibrary = {
</span><span style="color:#323232;">   methodA: function() {...},
</span><span style="color:#323232;">   methodB: functiom() {...},
</span><span style="color:#323232;">   methodC: function() {...},
</span><span style="color:#323232;">   methodD: function() {...}
</span><span style="color:#323232;">}
</span>

How would I split these methods up into modules and split up this code into multiple files to make it a standard file structure for a NPM package. Currently all of the code is in one large JS file, and it could be broken down into modules. I would like to keep it all in the one object myLibrary like most NPM packages which when used, have the following syntax…


<span style="color:#323232;">myLibrary.methodB(true, true);
</span>

Were all the methods are contained inside a object.

Any help will be most appreciated.

walter_wiggles,

I would recommend checking out Rollup (rollupjs.org/introduction/).

Rollup is one option that will fit a lot of situations, but there are other ways to bundle a library/module that focus more on how you want to use it. Is it used by other libs, by a UI application, etc.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • javascript@programming.dev
  • tacticalgear
  • DreamBathrooms
  • InstantRegret
  • magazineikmin
  • osvaldo12
  • Youngstown
  • ngwrru68w68
  • slotface
  • everett
  • rosin
  • thenastyranch
  • kavyap
  • GTA5RPClips
  • tester
  • JUstTest
  • modclub
  • cubers
  • ethstaker
  • mdbf
  • khanakhh
  • Durango
  • normalnudes
  • Leos
  • cisconetworking
  • anitta
  • provamag3
  • megavids
  • lostlight
  • All magazines