<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Leticia Mirelly ϟ on Medium]]></title>
        <description><![CDATA[Stories by Leticia Mirelly ϟ on Medium]]></description>
        <link>https://medium.com/@leticia-mirelly?source=rss-7deace8b3a8e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*B_p9l2m61TLynuXPBGv-qw.jpeg</url>
            <title>Stories by Leticia Mirelly ϟ on Medium</title>
            <link>https://medium.com/@leticia-mirelly?source=rss-7deace8b3a8e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 00:00:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leticia-mirelly/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Enhancing Front-End Decision-Making: Navigating the Choice Between Nuxt.js and Vue.js]]></title>
            <link>https://leticia-mirelly.medium.com/enhancing-front-end-decision-making-navigating-the-choice-between-nuxt-js-and-vue-js-26ff459f0f74?source=rss-7deace8b3a8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/26ff459f0f74</guid>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Leticia Mirelly ϟ]]></dc:creator>
            <pubDate>Fri, 12 Jan 2024 04:06:01 GMT</pubDate>
            <atom:updated>2024-01-12T13:26:42.604Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FgzsSUJEJQntC4lUHkq%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExYWNkaXN6NXh6ZHhzY2F3cjMwcTZoMnNvMGRvYWVwaXQwemk1bmI1dyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2FgzsSUJEJQntC4lUHkq%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExdTBpczl5dGp3OTc1NjE2NDRlM3RicDZ2NGw0cXEwamJsZHZpMWVqcCZlcD12MV9naWZzX2dpZklkJmN0PWc%2FgzsSUJEJQntC4lUHkq%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="246" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b629623f8be94d54e2cc499aff3ed44/href">https://medium.com/media/7b629623f8be94d54e2cc499aff3ed44/href</a></iframe><blockquote>Let me give you a small introduction before I begin</blockquote><h3><strong>Brief Introduction to Vue.js</strong></h3><p>Vue.js was originally created by Evan You in 2014 and has since garnered significant contributions from the community. It emerged in the market as a lightweight and intuitive alternative to the dominant frontend frameworks such as Angular and React. Its value proposition lies in combining an easy learning curve with powerful reactivity and flexibility. The Vue.js 3 team includes seasoned engineers, many of whom have contributed to other popular frameworks and libraries, bringing a diversity of experiences to the project. The source code and repository of Vue.js 2 are maintained in a central repository. While the Vue.js 2 source code is well-structured, the monolithic nature of the repository can pose challenges in managing new features and experiments. On the other hand, Vue.js 3 adopted a modular approach and used a monorepo for code management, which has facilitated maintenance, testing, and the addition of new features.</p><h4>Component-Oriented Architecture</h4><p>Component-Oriented Architecture Vue.js extends HTML with a template syntax that allows for the declarative expression of DOM rendering based on the state of the application’s data. To render the user interface, Vue.js uses a Virtual DOM — a lightweight representation of the real DOM. This enables Vue.js to calculate the minimal changes needed to update the UI without re-rendering the entire DOM tree. Vue.js implements a highly optimized “diff” algorithm that compares the Virtual DOM with a previous version to detect the exact differences. This results in a minimum number of DOM manipulations. The declarative templates are compiled into JavaScript rendering functions. These functions return Virtual DOM trees whenever the component data changes, which is excellent for performance.</p><h4>Reactivity</h4><p>There are two ways to intercept property access in JavaScript: getters/setters and Proxies. Vue 2 exclusively used getters/setters due to browser support limitations. In Vue 3, Proxies are used for reactive objects, and getters/setters are used for refs.Reactivity based on Proxies reduces the initialization overhead of components. In Vue.js 2, the need to recursively convert all properties of an object into getters/setters increased the startup time, particularly for large datasets.Projects in Vue.js 2 typically rely on tools like Webpack and Vue Loader to process .vue files and generate bundles. This approach can depend on specific configurations and plugins to work correctly. Vue.js 3 is developed and maintained as a monorepo, which includes sub-packages like @vue/runtime-dom and @vue/reactivity. This allows projects to import only the parts of Vue they need, reducing the size of the final bundle. With Vue.js 3, there is a growing movement towards the use of more modern and efficient build tools like Vite, which leverages ES modules for faster development.</p><h4>Some comparisons with other frameworks</h4><p>Vue.js offers a declarative approach to building UIs that is more straightforward and less verbose than Angular. Compared to React’s JSX, Vue’s templates are closer to HTML, making them more accessible to beginners and developers with strong HTML/CSS experience.</p><p>Vue.js introduced the concept of Single-File Components (SFCs), which encapsulate a component’s template, logic, and styles in a single file. This approach simplifies code organization and enhances clarity compared to the separate methods of managing template and logic in frameworks like Angular. The Vue ecosystem includes official solutions for routing (Vue Router) and state management (Vuex) that are designed to work seamlessly with Vue core. React and Angular also have strong ecosystems, but Vue benefits from tighter integration and a less complex state management model compared to Redux in React.</p><h4><em>For new projects, it’s highly recommended to use the latest version of Vue.js, which, at the moment, is </em><a href="https://blog.vuejs.org/posts/vue-3-one-piece"><em>Vue.js 3</em></a><em>. This version brings significant improvements in terms of performance, enhanced reactivity through the Proxy-based system, the Composition API for better code organization, along with other advanced features.</em></h4><h4><em>If migrating to Vue.js 3 is not feasible at the moment, maintaining the latest minor or patch version of Vue.js 2 is advisable, especially to ensure security and compatibility with current libraries.</em></h4><p><em>If you missed it, Vue.js 2 became obsolete along with the packages on npm since December 31, 2023:</em></p><ul><li><em>All major and minor versions of Vue 2 core</em></li><li><em>Versions of vue-router with exclusive support for Vue 2 (3.x and below)</em></li><li><em>Vuex versions with exclusive support for Vue 2 (3.x and below)</em></li></ul><p><em>More details can be found at: </em><a href="https://blog.vuejs.org/posts/vue-2-eol"><em>https://blog.vuejs.org/posts/vue-2-eol</em></a></p><h4>Boilerplates for Creating Vue.js Projects</h4><p><strong><em>Vite</em></strong> is a next-generation build tool that significantly improves the development experience. It uses Rollup for production builds and features an extremely fast development server based on native ES modules. It is ideal for projects seeking speed in the development and build process. Its rapid hot-module reloading (HMR) and simplified configuration make it suitable for modern SPAs and Vue.js projects that demand high-performance.</p><p><strong><em>Vue CLI</em></strong> is the standard tool for creating Vue.js projects. It offers a rich set of configuration options, plugins, and a GUI for project management. It is well-suited for developers who prefer a more traditional and comprehensive setup for their Vue.js applications.</p><p><strong><em>Quasar</em></strong> is a Vue.js framework that enables developers to build SPA, SSR, PWA, Mobile (using Cordova or Capacitor), and Electron applications with a single codebase. It is best for projects that require multiple platform/rendering solutions with a unified codebase, providing versatility and efficiency in development.</p><p>In addition to these, there are other notable templates and frameworks in the Vue.js ecosystem, each offering unique features and benefits to cater to different needs and preferences in web and application development.</p><h3><strong>Brief Introduction to Nuxt.js</strong></h3><p>Nuxt.js was created by French brothers Sébastien and Alexandre Chopin, with its first version released in 2016. The goal was to create a framework that simplified the development process with Vue.js, focusing particularly on server-side rendering (SSR) and automatic configuration.</p><p>Nuxt.js is particularly valued for its ability to improve SEO (Search Engine Optimization) and the initial page loading performance, which are common challenges in Single Page Applications (SPAs). It simplifies the implementation of SSR by automating much of the process. This includes setting up the server, rendering Vue.js components, and managing synchronization between server and client. It also offers the capability to generate a static site. This means that all application pages are pre-rendered during the build process, generating static HTML files.</p><p>From the outset, Nuxt.js was developed to complement and enhance Vue.js’s capabilities. For example, automatic routing is one of Nuxt.js’s most attractive features, eliminating the need to manually define application routes using Vue Router. During the build process, Nuxt.js automatically generates the Vue Router configuration based on the structure of the ‘pages’ directory.</p><p>Nuxt.js utilizes a specific directory structure that includes pages, components, layouts, plugins, middleware, assets, and static. This structure is an integral part of Nuxt.js’s convention-over-configuration approach. Plugins are frequently used to add global libraries (like Axios for HTTP requests or Vuetify as a UI library) or to inject global functions and constants.</p><h4>Applications with Strong SEO Needs</h4><p>For applications that rely on good visibility in search engines, like blogs and e-commerce sites, server-side rendering (SSR) is essential. SSR ensures that the content is fully rendered on the server side, making it more easily indexable by search engines, which is crucial for SEO.</p><h4>High-Performance Websites</h4><p>For websites where loading performance is crucial, such as portfolios or corporate pages, static site generation offers the best solution. Static sites are faster to load and more efficient in terms of server resource usage because the HTML is pre-rendered and ready to be served instantly.</p><p>Nuxt.js uses Webpack as its module bundler. During the build process, Webpack processes and packages resources such as JavaScript, Vue components, CSS, and images. This bundling is vital for optimizing application performance, as it reduces the number of requests needed to load a webpage and can include optimizations like minification and tree shaking.</p><h4>Dependency Management and Versioning</h4><p>Nuxt.js also adopts SemVer, and each version of Nuxt is typically built to work with a specific version of Vue.js. For instance, Nuxt.js v2 is compatible with Vue.js v2.x, while Nuxt.js v3 is designed to work with Vue.js v3. This ensures that when a developer chooses a specific version of Nuxt.js, they can be confident about which version of Vue.js it is compatible with, which is crucial for maintaining consistency and stability in application development.</p><h4>Evolution of Nuxt.js: From Early to Current Versions</h4><p><a href="https://medium.com/@nuxt_js/nuxt-js-1-0-is-out-bab1af459972"><strong><em>Nuxt.js 1.x</em></strong></a><strong> </strong>The early versions of Nuxt.js focused on providing a framework for Vue.js applications with server-side rendering (SSR) out-of-the-box. This approach simplified the development of SSR and SPA (Single Page Application) applications, making it easier for developers to create complex, SEO-friendly web applications without dealing with the intricacies of server-side rendering configurations.</p><p><a href="https://medium.com/@nuxt_js/nuxt-js-2-0-webpack-4-esm-modules-create-nuxt-app-and-more-6936ce80d94c"><strong><em>Nuxt.js 2.x</em></strong></a><strong><em> </em></strong>Updates in this series brought significant improvements, such as Static Site Generation (SSG) and automatic optimization for performance. There were also advances in modularity and extensibility, with the introduction of Nuxt.js modules that could add functionality to the framework without the need for extensive manual configuration. This version made Nuxt.js more flexible and powerful, catering to a wider range of web development needs.</p><p><a href="https://nuxt.com/docs/getting-started/installation"><strong><em>Transition to Vue.js 3</em></strong></a> The introduction of Vue.js 3, with features like the Composition API, Proxy-based reactivity, and enhanced TypeScript support, necessitated corresponding adaptations in Nuxt.js. This led to the development and eventual release of Nuxt.js 3, which was a significant step in keeping the framework aligned with the latest Vue.js features and improvements.</p><p><strong><em>Nuxt.js 3 and Beyond</em></strong><em> </em>Nuxt.js 3 was designed to leverage all the new functionalities of Vue.js 3. It offers a rewrite of the core to use the Composition API, improves support for TypeScript, and incorporates performance enhancements from the new version of Vue.js. This version represents a major leap forward in terms of developer experience, performance, and capabilities, making it a robust choice for modern web development.</p><h3>Detailed Comparison</h3><p>When comparing Vue.js and Nuxt.js, it’s crucial to understand that they are not direct competitors but complementary to each other. Vue.js is a progressive framework for building user interfaces, while Nuxt.js is a framework based on Vue.js, designed to simplify the development of universal (SSR) and single-page applications (SPAs).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*rKtYy2yw9tjc6IgWLGyAmA.png" /></figure><h4>Main Use</h4><ul><li><strong>Vue.js</strong> is ideal for building dynamic user interfaces. <strong>Nuxt.js</strong>, being a layer on top of Vue.js, is more suitable for projects that require server-side rendering or static site generation.</li></ul><h4>Routing</h4><ul><li><strong>Vue.js</strong> offers flexibility in routing but requires manual configuration. <strong>Nuxt.js</strong> automates this process, making routing easier based on the directory structure.</li></ul><h4>SEO and Performance</h4><ul><li><strong>SPAs in Vue.js</strong> may face challenges with SEO due to their client-side nature. <strong>Nuxt.js</strong> addresses these issues with SSR and SSG, offering better SEO practices and faster initial loading times.</li></ul><h4>Project Structure</h4><ul><li>While <strong>Vue.js allows for a more flexible approach to project structuring</strong>, <strong>Nuxt.js offers a more standardized project structure</strong>, which can speed up development, especially in larger projects.</li></ul><p>When evaluating Vue.js and Nuxt.js from the perspectives of <strong>scalability</strong>, <strong>maintainability</strong>, and <strong>performance</strong>, we are considering how these technologies behave as applications grow in complexity and size, and how they facilitate ongoing maintenance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*Wa2ri_btTNzQAUmgUpiEOg.png" /></figure><h3>Nuxt.js Case Study: Large-Scale E-Commerce</h3><h4>Platform Developing a robust e-commerce platform, with thousands of products, high user customization, and strict SEO and performance demands.</h4><p><strong><em>Server-Side Rendering (SSR) for SEO and Performance:</em></strong> Traditional SPAs face challenges with SEO and initial load time.<strong><em> Solution:</em></strong> With native SSR, Nuxt.js significantly improves SEO and page loading speed, which is crucial for an e-commerce site.</p><p><strong><em>Static Site Generation (SSG) for Product Pages:</em></strong> Maintaining performance with a large number of product pages. <strong><em>Solution:</em></strong> Using SSG, Nuxt.js can generate static pages for each product, reducing server load and improving loading speed. Project Structure and Automatic Routing.</p><p><strong><em>Managing a large codebase and complex routing. Solution:</em></strong> Nuxt.js’s project structure and automatic routing simplify code organization and route management, increasing development efficiency.</p><p><strong><em>Modularity and Plugins:</em> </strong>Integrating various features such as payment systems, inventory management, and user customization. <strong>Solution:</strong> The modular architecture of Nuxt.js and support for plugins allow for clean and maintainable integration of complex functionalities. Internationalization and Localization</p><p><strong><em>Adapting the platform for multiple languages and regions. Solution:</em></strong> With the nuxt-i18n module, the platform can easily support multiple languages, facilitating content localization.</p><h4><strong>Results and Benefits</strong></h4><p><strong><em>Improved User Experience: </em></strong>The application loads quickly, even with a large amount of dynamic content, thanks to SSR and SSG.</p><p><strong><em>Simplified Maintenance:</em> </strong>Maintenance is easier due to the organized structure and the ease of updates and integrations.</p><p><strong><em>Assured Scalability:</em></strong> The site is able to handle an increase in traffic and product quantity without sacrificing performance.</p><h3>Vue.js Case Study: Analytics Dashboard Platform</h3><h4><strong>Create an interactive analytics dashboard for a medium-sized company, providing real-time insights into sales data, customer engagement, and product performance.</strong></h4><p><strong><em>Rapid and Iterative Development. Solution:</em></strong> With Vue CLI, the project can be quickly bootstrapped, and Vue.js’s declarative syntax and reusable components allow for fast iterative development.</p><p><strong><em>Performance and Optimization:</em></strong> Ensuring smooth performance with large volumes of data. <strong><em>Solution</em></strong>: Vue.js offers an efficient reactive system and Virtual DOM to optimize UI updates, maintaining performance even with large data volumes.</p><p><strong><em>Scalability and Maintenance:</em> </strong>The application must be easy to scale and maintain.<strong><em> Solution: </em></strong>The modular and component-based architecture of Vue.js, along with an active community and excellent documentation, facilitate scalability and maintenance.</p><h4>Results and Benefits</h4><p><strong><em>Development Agility:</em></strong> The prototype was developed and iterated quickly, thanks to the ease of use of Vue.js.</p><p><strong><em>Optimized Performance: </em></strong>The dashboard maintained high performance, even when handling and displaying data in real-time.</p><h3>Guidelines for Efficient Code Architecture</h3><h4>Modularity and Encapsulation</h4><p><strong><em>Component-Oriented Design: </em></strong>Divide the application into smaller, independent components. This not only facilitates reuse and maintenance but also allows for more efficient testing.</p><p><strong><em>Component Encapsulation:</em></strong> Use encapsulation to hide the internal complexity of components, exposing only clear and well-defined interfaces.</p><h4>Clean and Separated Architecture</h4><p><strong><em>Model-View-ViewModel (MVVM) Pattern: </em></strong>Separate business logic (Model), presentation layer (View), and presentation logic (ViewModel). This promotes efficient decoupling and facilitates unit testing.</p><p><strong><em>Service Layer:</em></strong> Introduce a service layer to abstract business logic and interactions with external APIs or the backend.</p><h4>State Management Patterns</h4><p>State management patterns are crucial in frontend applications, especially as complexity and size increase. These patterns provide an organized framework for managing and maintaining application state predictably and efficiently.</p><p><strong><em>Vuex </em></strong>is a state management pattern and library for Vue.js applications. It serves as a centralized store for all components in an application. Features: Deep integration with Vue’s reactivity system. Concepts of state, getters, actions, and mutations. Facilitates the maintenance and management of shared states in large Vue applications.</p><p><strong><em>Pinia</em></strong> is the latest addition to the Vue.js ecosystem as an alternative to Vuex for state management. Developed with Vue 3’s Composition API in mind, Pinia offers a more intuitive and flexible state management experience. <strong><em>Stores as State Sources</em> </strong>In Pinia, each “store” is essentially a source of reactive state. Stores are lighter and more flexible compared to Vuex stores. <strong><em>Hot Module Replacement (HMR),</em></strong> meaning store state can be preserved during hot-reloading during development. Easy to Learn and Use Pinia’s framework and API are simpler and more straightforward, making it easier to learn and use, especially for new Vue developers.</p><h4>Routing and Lazy Loading</h4><p>Implement dynamic routing and permission-based routing to manage access to different areas of the application.</p><h4>Advanced CSS Practices</h4><p>Adopt methodologies like BEM, SMACSS, or OOCSS to maintain scalable and maintainable styles.</p><h4>Automated Testing</h4><p>Implement unit tests, integration tests, and end-to-end (E2E) tests to ensure code quality.</p><h4>Linting and Code Formatting</h4><p>Use tools like ESLint and Prettier to maintain code consistency and identify potential issues early on.</p><blockquote>An efficient code architecture in frontend projects requires a holistic approach that encompasses everything from component structure to advanced state management practices and performance optimization. These guidelines are essential for creating scalable, efficient, and easy-to-maintain applications, suitable for the complex needs of web development.</blockquote><h3>Tips for optimizing code performance and maintenance: Strategies to keep code clean and performant</h3><h4>Clean Code Principles</h4><p><strong><em>Readability:</em> </strong>Write code as if it were a book. This includes descriptive variable and function names, as well as a clear logical structure.</p><p><strong><em>Simplicity: </em></strong>Avoid unnecessary complexity. Simpler solutions are often easier to maintain.</p><p><strong><em>DRY (Don’t Repeat Yourself): </em></strong>Eliminate code repetition. Reuse code whenever possible to reduce duplication and ease future changes.</p><p><strong><em>Efficient State Management (</em></strong><em>Local vs. Global State): </em>Carefully assess the need for global state. Use local state whenever possible to reduce complexity.</p><p><strong><em>Caching and Memoization: </em></strong>Implement caching and memoization for heavy operations, such as API queries or complex calculations.</p><h3>Conclusion</h3><h4>Features and Practices</h4><p><strong>Vue.js — Flexibility and Intuitiveness: </strong>Vue.js is ideal for SPAs and dynamic user interfaces, known for its smooth learning curve, componentization, and efficient reactivity.</p><p><strong>Nuxt.js — Structure and Optimization: </strong>Nuxt.js extends Vue.js capabilities, offering standardized structures and additional features like SSR and SSG, ideal for projects requiring enhanced SEO and performance.</p><p><strong>Design Patterns and Code Architecture: </strong>Adopting clear design patterns and efficient code architecture, such as modularity, encapsulation, and state management, is crucial for scalability and maintainability.</p><h3><strong>Choosing between Vue.js and Nuxt.js for a specific project depends on various factors related to the project’s needs and objectives. Here are some final guidelines to help make this decision:</strong></h3><h4>Considering the Scope and Project Requirements</h4><p><strong>Vue.js: </strong>Ideal for projects that require a flexible and customizable approach, such as Single Page Applications (SPAs) or when integrating Vue into an existing project.</p><p><strong>Nuxt.js:</strong> Better suited for projects that benefit from Server-Side Rendering (SSR) or Static Site Generation (SSG), such as applications that need improved SEO and better initial loading performance.</p><h4>Evaluating Team Experience and Available Resources</h4><p><strong>Learning Curve: </strong>Teams already familiar with Vue.js will find it easy to adopt Nuxt.js, but for those who are new to both, Vue.js can be a more accessible starting point due to its smoother learning curve.</p><p><strong>Resources and Community:</strong> Both have active communities and a good number of available resources. Considering community support and resources can be crucial, especially for smaller teams or projects with limited budgets.</p><h4>SEO and Performance</h4><p>If SEO and initial loading performance are priorities, Nuxt.js with its integrated SSR and SSG is the most suitable choice.</p><h4>Flexibility vs. Convention</h4><p><strong>Vue.js: </strong>Offers greater flexibility in structuring the project. <strong>Nuxt.js:</strong> Provides directory structure conventions that can speed up development and ensure consistency, especially useful in larger teams or projects with many developers.</p><p><em>The choice between Vue.js and Nuxt.js should be based on a careful assessment of the project’s specific needs, available resources, and the development team’s experience. Vue.js is better suited for projects that require a flexible and customizable approach, while Nuxt.js is ideal for projects that need advanced features like SSR and SSG for SEO and performance. Evaluating these factors will help ensure that the chosen technology aligns with the project’s goals and requirements, maximizing development efficiency and success.</em></p><blockquote>Feel free to <a href="https://www.linkedin.com/in/leticiamirelly/">connect with me on LinkedIn</a> for any further discussions or questions you may have. Happy coding!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26ff459f0f74" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Comprehensive Guide to npm Workspaces and Monorepos]]></title>
            <link>https://leticia-mirelly.medium.com/a-comprehensive-guide-to-npm-workspaces-and-monorepos-ce0cdfe1c625?source=rss-7deace8b3a8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce0cdfe1c625</guid>
            <dc:creator><![CDATA[Leticia Mirelly ϟ]]></dc:creator>
            <pubDate>Sat, 30 Dec 2023 14:42:30 GMT</pubDate>
            <atom:updated>2024-08-01T12:33:55.886Z</atom:updated>
            <content:encoded><![CDATA[<h3>A little about current workspace managers</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FtHufwMDTUi20E%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fschool-back-guinea-tHufwMDTUi20E&amp;image=https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExNTdxMjhkZm5yM3l0MjlhdGx5MW0yczdoOTg4ZzY4bTVhZ3R4MTVjZCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2FtHufwMDTUi20E%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/86b9287d3e6c7c18976c8b8921174338/href">https://medium.com/media/86b9287d3e6c7c18976c8b8921174338/href</a></iframe><h4>Yarn Classic x Yarn Berry Workspaces</h4><p><strong>Dependency Resolution:</strong> Yarn classic workspaces use a “pluggable” resolution algorithm, providing more flexibility in how dependencies are resolved. Like npm, Yarn uses hoisting to share common dependencies at the root level, unlike Yarn Berry, which has a new architecture with a “plug’n’play” resolution strategy, aiming for a zero-installation approach. It takes advantage of the concept of “PnP” files to represent dependencies without the need for a traditional node_modules directory. This is a complex choice if you want to migrate managers later because Yarn Berry eliminates the need for a central node_modules leading to faster and more efficient installations however brings significant changes in how dependencies are resolved.</p><h4><strong>Npm Workspaces</strong></h4><p><strong>Dependency Resolution: </strong>npm workspaces utilize a “fixed” strategy for dependency resolution. Each workspace has its own node_modules, and dependencies are installed per workspace. However, common dependencies are hoisted to the root node_modules to optimize disk space.</p><p><strong>Established Ecosystem Integration:</strong> Being the default package manager for Node.js, npm seamlessly integrates with the broader Node.js ecosystem.</p><p><strong>Share Dependencies: </strong>npm Workspaces enable the sharing of dependencies between packages. If multiple packages require the same dependency, it can be installed once at the workspace level, optimizing disk space and simplifying dependency management.</p><h3>Hands-on with npm workspace</h3><h4>Setup Project</h4><pre>mkdir your-project &amp;&amp; cd your-project &amp;&amp; npm init -y</pre><h4>Parent Workspace Configuration (root package.json)</h4><pre>{<br>  &quot;name&quot;: &quot;my-monorepo&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;packages/*&quot;<br>  ],<br>// Example of shared scripts in workspaces<br>{<br>  &quot;scripts&quot;: {<br>    &quot;build&quot;: &quot;npm run build:package-a &amp;&amp; npm run build:package-b&quot;,<br>    &quot;build:package-a&quot;: &quot;cd packages/package-a &amp;&amp; npm run build&quot;,<br>    &quot;build:package-b&quot;: &quot;cd packages/package-b &amp;&amp; npm run build&quot;<br>  }<br>}<br>}</pre><h4>Directory Structure</h4><pre>/my-monorepo<br>|-- /packages<br>|   |-- /package-a<br>|   |-- /package-b<br>|-- package.json</pre><h4>Package A</h4><pre>// /packages/package-a/package.json<br>{<br>  &quot;name&quot;: &quot;package-a&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: run tests from root\&quot; &amp;&amp; exit 1&quot;<br>  }<br>}</pre><pre>// /packages/package-a/index.js<br>module.exports = () =&gt; {<br>  console.log(&#39;Hello from Package A!&#39;);<br>};</pre><h4>Package B</h4><pre>// /packages/package-b/package.json<br>{<br>  &quot;name&quot;: &quot;package-b&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;package-a&quot;: &quot;1.0.0&quot;<br>  },<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: run tests from root\&quot; &amp;&amp; exit 1&quot;<br>  }<br>}</pre><pre>// /packages/package-b/index.js<br>const packageA = require(&#39;package-a&#39;);<br><br>module.exports = () =&gt; {<br>  packageA();<br>  console.log(&#39;Hello from Package B!&#39;);<br>};</pre><h3>npm link in Workspaces:</h3><h4><strong>Local Development and Testing:</strong></h4><p>npm link</p><p>is a powerful tool in the npm ecosystem, allowing packages to be symlinked for local development. In the context of npm workspaces, this tool can be used to connect packages within the workspace for seamless local development and testing.</p><h4><strong>Mechanism:</strong></h4><p>npm ensures that when using npm link, the linked package is correctly hoisted to the top-level node_modules directory, maintaining the integrity of the workspace structure. After running the above command, package-a will be symlinked and hoisted to the top-level node_modules, allowing seamless development.</p><pre># Example npm link in workspaces<br>cd packages/package-a<br>npm link<br>cd ../package-b<br>npm link package-a</pre><p>After setting up the structure, running npm install in the root directory installs all dependencies for all packages, linking them where necessary.</p><h3>Helpful Commands for Reference</h3><h4><strong>Installing Workspace Dependencies</strong></h4><p>Install dependencies for all packages defined in the workspace configuration.</p><pre>npm install</pre><h4>Scopes and Private Packages in Workspaces</h4><ul><li>Scopes (@scope/package) can also be used in workspaces, and the logic of private scopes still applies.</li></ul><pre># Example of installation in workspaces with private scope<br>npm install @scope/package-a --workspace</pre><h4>Listing Dependencies Across Workspaces</h4><p>List dependencies across all workspaces.</p><pre>npm ls</pre><h3>Hoisting in npm Workspaces</h3><p>In npm workspaces, hoisting plays a crucial role in optimizing the installation and management of dependencies across multiple packages within the same workspace.</p><h4>Common Dependencies</h4><ul><li>When multiple packages within a workspace depend on the same package and version, npm hoists that dependency to the top-level node_modules directory. This means that instead of having duplicate copies of the same dependency in each package&#39;s node_modules, there&#39;s a single shared instance at the root.</li></ul><pre>// Example workspace package.json<br>{<br>  &quot;workspaces&quot;: [&quot;packages/*&quot;],<br>  &quot;dependencies&quot;: {<br>    &quot;lodash&quot;: &quot;^4.17.21&quot;<br>  }<br>}</pre><p>If two packages within the workspace use lodash, npm ensures that only one copy of lodash is present at the top-level node_modules directory.</p><h4>Scoped Packages</h4><ul><li>For scoped packages, hoisting also occurs within the scoped directory, further optimizing the organization of dependencies.</li></ul><pre>// Example workspace package.json with scoped package<br>{<br>  &quot;workspaces&quot;: [&quot;packages/*&quot;],<br>  &quot;dependencies&quot;: {<br>    &quot;@my-monorepo/package-a&quot;: &quot;^1.0.0&quot;<br>  }<br>}</pre><p>The scoped package, in this case, will be hoisted to the top-level node_modules/@my-scope/package-a directory.</p><h3>Benefits of Hoisting in npm Workspaces:</h3><p><strong>Disk Space Efficiency</strong></p><p>By hoisting common dependencies, npm workspaces optimize disk space usage, as there’s only one copy of a dependency shared among multiple packages.</p><p><strong>Faster Installations</strong></p><p>Hoisting reduces the time it takes to install dependencies, as npm can avoid redundant installations and copy operations.</p><p><strong>Consistent Dependency Versions:</strong></p><p>Hoisting ensures that all packages within the workspace use the same version of a dependency, avoiding version conflicts.</p><h3>Conclusion</h3><p>While it is feasible to manage tasks at the individual workspace level, centralizing shared responsibilities at the root level fosters a more coherent and straightforward approach. Embracing npm Workspaces can significantly improve monorepo management, fostering enhanced collaboration, modularity, and overall development efficiency. However, the choice between npm Workspaces, Yarn Workspaces, and Yarn Berry hinges on considerations such as ecosystem familiarity, project requirements, and personal preferences. Each of these tools possesses unique strengths, and the decision should be carefully aligned with the specific needs and objectives of the project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce0cdfe1c625" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>