How to use executables installed in the npm root of a monorepo from anywhere?

Viewed 460

This question (How to use executables from a package installed locally in node_modules?) asked this years ago, and the correct answer is generally just npx these days. But in a monorepo with multiple subpackages, with different dependencies in each, although it works, it is quite annoying.

my_pkg_root/ package.json
   sub_pkg_1/ package.json
   sub_pkg_2/ package.json
   sub_pkg_3/ package.json

Or other more deeply nested structures. If in the root you install npm i myutils providing doit, then in my_pkg_root, npx doit just works. In the subpackages, what npx (apparently) does is it first installs myutils in the local node_modules, runs the script, and then UNINSTALLs it. Every time.

I want a way that (using npx or other techniques) I can run doit in any subdirectory without all the annoying overhead.

1 Answers

Extending the idea from this answer (https://stackoverflow.com/a/15157360/500902) to the earlier question, if you know the monorepo's root directory name (seems likely, here assuming /internal)

alias basex='_x=$(pwd); PATH="${_x%%/internal/*}/internal/node_modules/.bin:$PATH"'

Then from anywhere in the monorepo, you can do

basex doit
Related