And the second big feature I wanted to cover today was this one. Up until now, peer dependencies had to be installed manually. That’s not the case anymore though. But first, what exactly are peer dependencies?
With the command and folder structure given so far, you’ll end up with all three modules (and their required dependencies) installed inside the node_modules folder located at the root level. However, any file inside its hierarchy will be able to access all three of them.
We can now run npm install from the root folder and what do you think will happen? It will install everything each of these workspaces requires inside a generic node_modules folder. And because of that, repeated dependencies will not be duplicated.
Essentially, NPM will now do the developer’s job and help the latter decide whether they should install that peer dependency or not. This is definitely a very useful feature if you consider the amount of work dependency management can add to your day.
You can see how the JSON files for each API folder only really differ on their name and their dependencies. The same goes for the core folder, where we declare Express as their main shared dependency:
This would mean you end up installing React twice, with different versions. All because you needed module A . Now, however, thanks to Arborist, the full tree is analyzed and peer dependencies are taken into account, which means that if a conflict is going to be present, a proper error will be displayed and the process will be aborted.
How do we achieve this? Once you understand how it works, the configuration process is actually quite straightforward. The point here is that you need to define a package.json file at the root level (inside the REST-APIS folder) where you declare the workspaces. With that ready, all you have to do is create one package.json inside each individual project, and declare their required dependencies there.
This is all good in theory, but the problem with automatically installing these dependencies was that when you added two packages which had the same dependency but with different versions, both versions would get installed (one of them at the regular node_modules folder and the other, as a sub dependency of the package that requires it).
That is all you need, you can, of course, define other properties, but for the purposes of this new feature, the “workspaces” key is all you need. Inside of it, you can define (as you can see in the example) a list of paths (with the included wildcard format as well) that reference the folders where your workspaces will live.
Version 7 of NPM was released with several new features and improvements. These two particular ones were the ones that definitely caught my attention and got me really excited to try them. Workspaces have the potential to radically improve the developer’s experience when working on large, combined projects with multiple shared dependencies.
This could potentially cause incompatibility issues, imagine adding module A which depends on module B which in turn, depends on [email protected], into a project that, in turn, depends on [email protected] Because A needs version 15, it would add it as a dependency as well, ending with a dependency tree like:
Well, if you’re working on individual and unrelated projects, maybe workspaces aren’t really that useful to you. At any given point, the requirements for them could change, and that would render the benefits of the workspaces useless.
In case you’re not super familiar with the term (I know I wasn’t), peer dependencies are almost like normal dependencies, but instead of defining a strong requirement, they essentially state that:
However, if you’re working as part of a team on several related projects (maybe you’re crafting a microservices-based architecture), then workspaces might actually be a very needed feature. Just think about the amount of disk space you’d be saving if you have an architecture of 100 microservices, all of them depending on the same set of modules. That’s where this feature really starts to shine!
- Megadede, cuevana, peliculas 2019, películas 2019, cuevana 3, mientras dure la guerra, mejores peliculas 2019, mejor película 2019, mejores películas 2019