{"id":2285,"date":"2025-09-19T12:24:11","date_gmt":"2025-09-19T12:24:11","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=2285"},"modified":"2026-02-05T11:59:13","modified_gmt":"2026-02-05T11:59:13","slug":"best-ways-to-share-dependencies-in-angular-mfes","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/best-ways-to-share-dependencies-in-angular-mfes\/","title":{"rendered":"Best Strategies for Sharing Dependencies in Angular Micro Frontends with Module Federation"},"content":{"rendered":"\n<p>When you&#8217;re building micro frontend applications with Angular and Webpack 5, dependency sharing becomes critical. Without it, you risk bloated bundles, redundant code, and version conflicts. Module Federation offers built-in tools to help manage this, but knowing how to use them well makes all the difference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Core Concepts: Host and Remote<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Host Application (Shell): <\/strong>Acts as the container and controls layout and routing<\/li>\n\n\n\n<li><strong>Remote Application:<\/strong> Independent Angular apps that expose modules, components, or services to the host<\/li>\n<\/ul>\n\n\n\n<p>Module Federation allows dynamic runtime loading of remotes by the host, making it possible for teams to build and deploy features independently.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Power of the shared Configuration<\/h2>\n\n\n\n<p>Webpack\u2019s ModuleFederationPlugin has a shared property that tells it which libraries should be used by both the host and remote apps. This is helpful in microfrontends because it stops the same library from being loaded more than once on the page. For example, you can make sure Angular or React is shared, so it isn\u2019t duplicated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Strategies for Sharing Dependencies<\/h2>\n\n\n\n<p>There are several effective strategies for managing shared dependencies in an Angular micro frontend setup. The choice of strategy often depends on the specific requirements of your project, the size of your teams, and your overall architectural goals.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Strategy<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Pros<\/strong><\/td><td><strong>Cons<\/strong><\/td><\/tr><tr><td><strong>Share All<\/strong><\/td><td>Shares all dependencies from the host with all remotes.<\/td><td>Easy setup, less config<\/td><td>Shares too much, bigger bundles<\/td><\/tr><tr><td><strong>Singleton (Granular)<\/strong><\/td><td>It shares only key libraries like Angular core as singletons.<\/td><td>Prevents errors, better performance<\/td><td>Manual setup, more effort<\/td><\/tr><tr><td><strong>Strict Versioning<\/strong><\/td><td>Enforces matching versions for shared packages.<\/td><td>Stable, catches version issues early<\/td><td>Can block remotes if versions clash<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Example: A Shell and a Remote Micro Frontend<\/h2>\n\n\n\n<p>Let&#8217;s walk through a practical example of setting up a host (shell) application and a remote micro frontend, sharing Angular dependencies.<\/p>\n\n\n\n<p><strong>Project Setup<\/strong><\/p>\n\n\n\n<p>We&#8217;ll create a new Angular workspace with two applications: a shell and a mfe1 (micro frontend).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create a new Angular workspace<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ng new angular-mfe-example --create-application=false\ncd angular-mfe-example<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Generate the shell and remote applications<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ng g application shell --routing --style=scss\nng g application mfe1 --routing --style=scss<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Adding Module Federation<\/h3>\n\n\n\n<p>Next, we&#8217;ll add the @angular-architects\/module-federation plugin to both projects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng add @angular-architects\/module-federation --project=shell --port=5000 --type=host\nng add @angular-architects\/module-federation --project=mfe1 --port=3000 --type=remote<\/code><\/pre>\n\n\n\n<p>This command will create webpack.config.js and webpack.prod.config.js files in both projects and update the angular.json to use a custom builder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Configuring the Remote (mfe1)<\/h3>\n\n\n\n<p>In the webpack.config.js of the mfe1 project, we&#8217;ll expose a module (e.g., FlightsModule) and define our shared dependencies.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ mfe1\/webpack.config.js\nconst { shareAll, withModuleFederationPlugin } = require('@angular-architects\/module-federation\/webpack');\n\nmodule.exports = withModuleFederationPlugin({\n\n  name: 'mfe1',\n\n  exposes: {\n    '.\/Module': '.\/projects\/mfe1\/src\/app\/flights\/flights.module.ts',\n  },\n\n  shared: {\n    ...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),\n  },\n});<\/code><\/pre>\n\n\n\n<p><strong>In this configuration:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>name: <\/strong>A unique name for the remote.<\/li>\n\n\n\n<li><strong>exposes: <\/strong>Defines which files from the remote are made available to the host.<\/li>\n\n\n\n<li><strong>shared:<\/strong> Utilizes the shareAll helper to share all dependencies from package.json as singletons with strict version checking.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. Configuring the Host (shell)<\/h3>\n\n\n\n<p>In the webpack.config.js of the shell project, we&#8217;ll define the remote and configure the shared dependencies.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ shell\/webpack.config.js\nconst { shareAll, withModuleFederationPlugin } = require('@angular-architects\/module-federation\/webpack');\n\nmodule.exports = withModuleFederationPlugin({\n\n  remotes: {\n    \"mfe1\": \"http:\/\/localhost:3000\/remoteEntry.js\",\n  },\n\n  shared: {\n    ...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),\n  },\n});<\/code><\/pre>\n\n\n\n<p>Here, the remote object maps the name of our remote (mfe1) to its remote entry file URL.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Lazy Loading the Remote Module<\/h3>\n\n\n\n<p>Now, in the shell application&#8217;s routing, we can lazy load the exposed module from mfe1.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ shell\/src\/app\/app-routing.module.ts\nimport { NgModule } from '@angular\/core';\nimport { RouterModule, Routes } from '@angular\/router';\n\nconst routes: Routes = &#91;\n  {\n    path: 'flights',\n    loadChildren: () => import('mfe1\/Module').then(m => m.FlightsModule)\n  }\n];\n\n@NgModule({\n  imports: &#91;RouterModule.forRoot(routes)],\n  exports: &#91;RouterModule]\n})\n\nexport class AppRoutingModule { }<\/code><\/pre>\n\n\n\n<p>The import path mfe1\/Module is a virtual path that Webpack understands thanks to the Module Federation configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. Running the Applications<\/h3>\n\n\n\n<p>Finally, run both the shell and mfe1 applications.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Generated bash\nng serve shell -o\nng serve mfe1```<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Sharing dependencies the right way can make or break your micro frontend architecture. Whether you go with a blanket approach or prefer granular control with singletons, it\u2019s essential to understand how these configurations affect load times and stability. If your team is moving toward a distributed architecture, now\u2019s a good time to <a href=\"https:\/\/www.cmarix.com\/hire-angular-developers.html\">hire Angular developers<\/a> who are well-versed in Module Federation and can help architect this cleanly from the start.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you&#8217;re building micro frontend applications with Angular and Webpack 5, dependency sharing becomes critical. Without it, you risk bloated bundles, redundant code, and version conflicts. Module Federation offers built-in tools to help manage this, but knowing how to use them well makes all the difference. The Core Concepts: Host and Remote Module Federation allows [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2286,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-2285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=2285"}],"version-history":[{"count":2,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2285\/revisions"}],"predecessor-version":[{"id":2289,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/2285\/revisions\/2289"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/2286"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=2285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=2285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=2285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}