Hey all!
Just curious if anyone has a work flow for streamlining internal code-reuse between projects via npm/yarn?
For example, most of my projects use sequelize
and have models like User
, CredentialChange
, Transaction
, etc. that I'd love to reuse internally.
Is there a best practice for using NPM to pull in that code (ideally still in a form that I can extend).
Top comments (4)
If you want to use npm to manage something you have to factor it out into a self-contained package. Once you do that you can use peerDependencies to ensure that sequelize or what have you has to be present alongside your "commons" library, but making it its own package is non-negotiable. If you're routinely extending your models with new fields it will probably be more trouble than it's worth.
create an NPM account, split your code into its own projects, and once you have everything setup locally (you can test your NPM package before deploying it), you publish it to Github and NPM. Shameless plug, you can check out my post about bootstrapping a project with Parcel for publishing
If you have multiple packages you think you'll be publishing I'd also look into github.com/lerna/lerna
Agree. Here's a good intro along with pros and cons of this approach. It also doesn't require you to publish your packages on NPM.