Then, to import apps/node_modules/shared/util.js from inside apps/node_modules/app1/server.js you
can use non-relative paths:
// apps/node_modules/app1/server.js
import util from 'shared/util.js'
The good news: this is fully supported with native NodeJS, so no bundler required.
The bad news: it feels a little clumsy, and some bundlers and tooling get really hung
up on the sub-folder named node_modules–they often filter by anything that has
node_modules in the path.
better solution
Using symlinks, you can use symlinks to point to the appropriate folders, which looks
like this:
So now we have the flat file paths again, without the embedded node_modules, and to import
shared/util.js from inside apps/app1/server.js we can also use non-relative paths:
// apps/app1/server.js
import util from 'prefix/shared/util.js'
this tool
This is a CLI tool that assumes the following folder structure (more about prefixes later):
Folders that are at the repo root level, and at each app level, that are prefixed
with the folderPrefix property, are symlinked.
Most of the projects that I work with use the underscore (aka _) character, like the
earlier example, eg. /_shared1 and /apps_folder/app1/_lib1.
Although it's common to have a single folder like _shared it is also common to have
multiple folders, e.g. one for services, one for controllers, etc.
npm prefix
Each of these folders is symlinked to a single "scope" name, e.g. the @angular/cli
scope name is @angular, so if you set --npmPrefix="@" the import name would be e.g.
@/shared or @/lib etc.
Specifically, the folderPrefix gets stripped from the folder name as part of the symlink.
So if you set the folderPrefix to _ and the npmPrefix to $ than if you had a
file at _controller/util.js you would import it with @/controller/util.js. Or if you
had a file at apps/app1/_lib/util.js you would import with @/lib/util.js.
Note: because of this naming convention, you cannot have an app symlinked folder,
e.g. apps/app1/_shared that has the same name (in this case _shared) as a root folder.
To do so would not be possible, so it'll throw an error.