Re-use personal modules via NPM/Yarn?

github logo ・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).

twitter logo DISCUSS (4)
markdown guide
 

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.

Classic DEV Post from May 31

Presentation Tips for Technical Talks

Presentation Tips for Technical Talks

Nicky Hajal profile image
Nicky's been a maker since his grandma bought him his first *real* toolbox when he was 4. His aunt taught him JavaScript when he was 12 and almost 20 years later, he still can't stop programming.

Still don't have a dev.to account? You're welcome to lurk, but it's better to be part of the community.

Sign up now ❤️