And of course, it gives you the NPM CLI, which is one of the best developer friendly CLIs that we have in the Node ecosystem. And this enhances, again, how you manage your monorepos because you now have a single CLI that can work across multiple boards in your monorepo. You also have access to the automatic installation and, again, interdependent packages within your repo. And I think I'll just add a link here for those who want to check the NPM Workspaces. But this has been a good try. I see this a lot in, again, such open source projects.
The next thing we want to eye on is what are the best practices for organizing code in the monorepo. Now, I added fine-grained models. And this is an advice that you should break down your code base into small and reasonable models that perform a single function or a feature. Example here, I have in this code image or code block, is a logger image that basically logs for information and it logs the error, right? And you can then import this across all the multiple applications in your monorepo. I also have here a validator, which again, is a regular expression that is for your email or your phone number. And again, this can be used across all the multiple files or the multiple packages or models or projects you have in your monorepo. And that's the excellency of organizing code in a Node.js monorepo. This is also what I add here, is a directory structure, which allows you to create a consistent directory structure, right? That basically reflects how your application is and the relationship between your packages or your applications or projects. Here in the code, we have a packages folder and in that particular folder, we have authentication, which has a source and a package.json. And next, in there again, the package folder, we have a user management, which handles all of the user management features in this particular project. We also have image service, which handles all of the image service of users in this particular project or monorepo. And this is an excellent way of organizing your directories, right? Because with this, you can then link the different package.jsons or even different applications across each other. For example, I can use the auth to work with user management. I can use authentication to work with the image service. I added here shared utilities and libraries. And now, this is in collaboration with our first one, right? Which is a modular, fine-grained library or approach. And now, with these, you can basically create, again, small size packages that can be imported and used across different packages or protocols or even projects.
The next on our slide is the scalability strategies for Node.js applications, right? Basically, what are the strategies you're going to use to implement a Node.js repo and make sure the application is also scalable, its performance improves the way it should and it's actually sustainable. And the first one is the duplication of dependencies. Now, we have packages and tools like npm-dedu or Yarns Automatic Duplication, which again helps you to reduce the size of your node models by resizing and flattening your dependencies. And now, what this does is that it helps you basically to speed up your installation time and to reduce your disk usage. Because what Node.js does is it takes four duplicate packages and removes them, allowing you to just use one single package when due, right? And this can seriously cut down on your node model sizes. The next thing I added here is selective dependency installation. Basically, when you use Yarn Workspace or npm workspace, you can install only one dependencies at the root level, right? Instead of duplicating them across multiple packages, like for example, you want to use probably like bcrypt or you want to use like bcrypt or any other packages, right? You can basically have one of these two on your Yarn Workspace npm package in your root level, and then use it across all the different packages, models and projects in this monorepo.
Comments