I have a project, which consists of one root node package containing subpackages linked together by npm link - these subpackages depend on each other (listed in package.json dependencies) and the structure basically looks like this:
-rootpackage
--subpackageA
--subpackageB
Lets say subpackageA has dependency on subpackageB, so I link them to avoid publishing/reinstalling subpackageB in subpackageA after every change in the source of subpackageB.
The link works just fine until I run npm update
in subpackageA, which causes the subpackageB to be unlinked.
Now, I see two options:
npm link
operation after each npm install
or npm update
to ensure the links are always present. This works with postinstall
in case of installation, but in case of an update the postinstall
is not called. I don't know any postupdate command for npm, which is to be called after update.Is there any way to make one of those options work or any other way to solve this problem ? I need to keep this and other links so we don't have to run npm link
after every installation/update. I can't really find information about this issue anywhere. Btw I am using Node 6.4.0 and NPM 3.10.3.
So the solution is to use Yarn Workspaces or maybe project like Lerna.
Yarn Workspaces is a utility that expects a structure similar to what was described in the question and which maintains the linking subpackages and root automatically. It is very easy to set up (just 2 lines in root package.json and executing yarn
for the first time) and after it you don't have to worry about upgrade
or install
at all, the links stay in place unless you delete them manually.
Lerna expands on that and provides you with additional tooling for managing multipackage projects. It can use Yarn Workspaces internally for the linking if you use yarn but it is not a requirement and works fine with npm. Just make sure to have Git because last time I checked Lerna didn't work with SVN or other VCSs.