loading...

Re-use personal modules via NPM/Yarn?

nickyhajal profile image Nicky Hajal ・1 min read

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).

Discussion

pic
Editor guide
Collapse
dmfay profile image
Dian Fay

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.

Collapse
jdmg94 profile image
José Muñoz

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

Collapse
nektro profile image
Meghan (she/her)

If you have multiple packages you think you'll be publishing I'd also look into github.com/lerna/lerna

Collapse
krofdrakula profile image
Klemen Slavič

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.