Often you'll find that you need utility methods or other functionality that might make sense as an external module that you could :tada: open source π, but the process can be painful:
- create a directory outside the project
- initialize a git repo
- create a repo on github
- set the repo's remote origin to the github repo
- write the code
- publish the module
- install the module in your application
- realize you need another feature/bug fix
- make change
- publish the module
- re-install the module in your application
goto
#8
It's actually a little bit better with npm link
,
but still. So instead, you say "screw it", create a util/
directory, start putting
scripts in there, and just require('../../../../util/my-foo')
.
We'll set up a node_modules/
directory under src/
so that you can require
anything under this directory as if it were a module
that had been installed from npm. So by putting it in src/node_modules/
, you can
require('my-foo')
instead of ../../../../util/my-foo
.
This src/node_modules/
directory would be separate from your top-level node_modules/
all your npm modules get installed. And unlike the top-level node_modules/
, you would
check this directory into source control.
The src/node_modules/
directory lives under your application so any changes you make here
are immediately reflected in your application and you don't have the overhead
of a separate project + repo.
You can now develop modules side-by-side with your application and they can evolve along with it until they are mature enough to stand on their own. You can then split the module out into it's own repository, π open source it π, and publish it to npm.
Then you npm install --save my-foo
and you're done. All your code was
already using require('my-foo')
, so nothing else needs to change.
Go forth and write modular code.