TL;DR How do I configure different "sub-modules" in a modular node.js
project to refer to one another as simply as possible?
I'm trying to wrap my head around local packages for NPM, specifically as they relate to a modular project.
I'm building a web app with a front end and a back end API. These need to share a package which exports simple models. My project directory structure looks like this:
package
├── api
│ ├── dist
│ │ └── <compiled files>
│ ├── node_modules
│ │ └── ...
│ ├── package.json
│ └── src
│ └── <source files>
├── application
│ ├── dist
│ │ └── <compiled files>
│ ├── node_modules
│ │ └── ...
│ ├── package.json
│ └── src
│ └── <source files>
└── models
├── dist
│ └── <compiled files>
├── node_modules
│ └── ...
├── package.json
└── src
└── <source files>
Both the API and application projects are going to use models, so I abstracted that code to a separate sub-module within my project.
I've read the documentation for npm link
and that seems to be the right approach because, as I understand it, it symlinks the package in the node_modules
dir. This gives access to the code as it exists right now, instead of installing a copy in node_modules
. Sounds like what I need, but there is a wrinkle: I'm working on this project from a couple of different places: my laptop, my office at work, and occasionally from home. In addition, others will be contributing to this project in the future.
I would like to make it as simple as possible for a new contributor to get up and running with development.
Currently, a new contributor goes through these steps:
- clone the repository
cd
into the models dir- run
npm install
npm link
- run
cd
into the api dir- run
npm install
npm link models
- run
cd
into the application dir- run
npm install
npm link models
- run
- start working
What I would like to do (and I think npm
should be capable of doing) is:
- clone the repository
cd
into the models dir- run
npm install
- run
cd
into the api dir- run
npm install
- run
cd
into the application dir- run
npm install
- run
- start working
I could write a script to do this, but it seems like an obvious use case for npm
and I suspect that it's probably capable of doing something like this. I think I may be overlooking something because I'm not finding it in the documentation.