Skip to content

Depending Dependably on a Monorepo in Node

I’m working with Node projects that include npm packages as dependencies. I’m used to the Ruby world where we can easily switch between including packages from hosted git repositories and local git working copies. I hoped it would be the same with Node, but it doesn’t seem so simple to me. I’m writing this guide to remind myself how to make this work.

Monorepo Package Dependency

When my Project includes a Package from a Monorepo, I need to know more things. In particular, I need to know that the Monorepo packages its content to $MONOREPO_ROOT/packages/$PACKAGE_NAME. When I depend on this package using a local git working copy, I need to install the local package from its ./packages/$PACKAGE_NAME directory, rather than from the root of the Monorepo.

$ cd $PROJECT_ROOT
$ npm install "$MONOREPO_ROOT/packages/$PACKAGE_NAME"

When I do this in the Project, npm installs whatever is at $MONOREPO_ROOT/packages/$PACKAGE_NAME into $PROJECT_ROOT/node_modules.

However, I need to install the Package’s dependencies, which means that I also need to run npm install on the Package.

$ cd $MONOREPO_ROOT/packages/$PACKAGE_NAME
$ npm install

This way, the Package’s dependencies are installed when I install the Package into my Project.

Should I install the Package’s dependencies first, then install the Package into the Project? Or should I install the Package into the Project, then install the Project Package’s dependencies within $PROJECT_ROOT/node_modules/$PACKAGE_NAME?