├── package.json └── packages ├── package-a │ └── package.json # Dependencies: `lodash` └── package-b └── package.json # Dependencies: `lodash`, `package-a`Īfter running npm install, your node_modules folder will look like this. If two of your packages depend on each other, they get the reference from there.įor example, if you have this structure. Your packages (the ones you created) also get symlinked in the root node_modules folder. This is done for performance reasons: if a dependency is shared by multiple packages, it gets saved only once in the root. Now, when you run npm install in a multi-package repository, npm’s dependency tree manager is smart enough to scan your folders looking for all dependencies to install.ĭependencies are hoisted, meaning they get installed in the root node_modules folder. I’ve also provided a repository on GitHub with some sample code from the examples. In this post, you will see how npm Workspaces work, how to get started, and a comparison with other Workspace implementations. ![]() This special type of repository is known as a monorepo. In projects like this, you usually have a complex dependency tree, with many packages depending on each other. You can find similarities between all three Workspace implementations.īut what are Workspaces for? Workspaces help us managing repositories with multiple packages - more than one package.json file. In fact, npm is not trying to reinvent the wheel. Other package managers such as Yarn and pnmp already ship with Workspaces for quite a while now. ![]() The 7th version of the package manager introduced Workspaces. Let’s have a look at those two dependency relations, which should cover all our use cases for now.The newest major release of npm, launched in October 2020, came out with a very anticipated feature (at least for me). Let’s assume our monorepo dependencies look like this: When you reach the point where even parallelizing is too slow, or just using too many resources, it will be time to work on the real optimization, to detect which packages are affected by changes in a commit and run the CI only for them. Workspace: // with pattern matching // you can also list them manually: Build only affected packages Here’s how it is organized for us:įolder structure: packages/ package-A package-B. Basic setup Folder structure and Workspaceįor the monorepo, you have to define the workspace in package.json. We tried to have many code snippets, so that the article is not only theoretical, but can also be used to quickly get started or transform your monorepo. In this article, we describe the incremental steps we’ve taken in the last 2 years to keep the CI fast as the number of developers in the team was multiplied by 4 or 5. ![]() Even though we are not looking back, with scaling come challenges, and there is still some significant work to be done in order to grow properly. ![]() It means one repository with several packages, not all the code in a single service.Īt Contentsquare, we’ve adopted monorepo early on for our Node.js/JavaScript code. This is the monorepo approach:Ī monorepo should not be confused with a monolith. The alternative is to keep them all in the same repository. This can be because you have adopted micro-services, micro-frontends, or at least to share code between frontend and backend.Īt which point, you will have to decide whether each package should have its own dedicated (git) repository. On your journey to growing your company and scaling your code base, you will create several packages in order to share code.
0 Comments
Leave a Reply. |