{"id":1649,"date":"2023-10-19T05:00:00","date_gmt":"2023-10-19T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1649"},"modified":"2023-10-05T14:37:27","modified_gmt":"2023-10-05T21:37:27","slug":"creating-a-library-with-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/creating-a-library-with-angular\/","title":{"rendered":"Creating a library with Angular"},"content":{"rendered":"\n<p>You&#8217;re probably used to creating Angular applications, but what about Angular libraries?<\/p>\n\n\n\n<p>First, it&#8217;s essential to define what an Angular library is. A library is a collection of components\/pipes\/directives or services bundled together that can be shared and used in multiple code repositories. As a result, Angular libraries are designed to be published on npm (publicly or privately) so they can be shared with other people externally or internally.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>If you want to start a new library from scratch, these Angular CLI commands will get it done:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"506\" height=\"109\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-15.png\" alt=\"\" class=\"wp-image-1650\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-15.png 506w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-15-300x65.png 300w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above commands create a <code>projects\/my-lib<\/code> folder in the workspace, with a sample component and service in it. The main difference between a library and an application is that a library exposes <strong>public features<\/strong> that can be imported into other libraries or applications. <\/p>\n\n\n\n<p>Such features are listed and exported in <code>public-api.ts<\/code> in the library folder. That&#8217;s where you decide what&#8217;s public\/private in your library code. In this example, the library is just one service:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"586\" height=\"172\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-20.png\" alt=\"\" class=\"wp-image-1655\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-20.png 586w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-20-300x88.png 300w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Then, to test or build your library, you can use regular Angular CLI commands such as:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"183\" height=\"59\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-16.png\" alt=\"\" class=\"wp-image-1651\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Once built, a library can be published to the public <a href=\"https:\/\/www.npmjs.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">npm repository<\/a> with one single command. This command has to be run from the <code>dist<\/code> folder where the compiled library can be found after running your <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/anti-pattern-not-using-production-builds-to-deploy-production-code\/\" target=\"_blank\" rel=\"noopener\" title=\"\">production build<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"156\" height=\"39\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-17.png\" alt=\"\" class=\"wp-image-1652\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-17.png 156w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-17-150x39.png 150w\" sizes=\"auto, (max-width: 156px) 100vw, 156px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Note that this command requires an <a href=\"https:\/\/www.npmjs.com\/signup\" target=\"_blank\" rel=\"noopener\" title=\"\">npm account<\/a> and npm authentication before you can publish. Upon publishing, the version number used in your <code>package.json<\/code> will be used as the public version number on npm:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"474\" height=\"148\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-18.png\" alt=\"\" class=\"wp-image-1653\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-18.png 474w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-18-300x94.png 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And on <a href=\"https:\/\/www.npmjs.com\/package\/ngx-signal-store\" target=\"_blank\" rel=\"noopener\" title=\"\">npm&#8217;s website<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"185\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-19.png\" alt=\"\" class=\"wp-image-1654\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-19.png 500w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-19-300x111.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>At that point, anyone can run <code>npm install [your-library-name]<\/code> and use your code in their projects. Nice and easy!<\/p>\n\n\n\n<p>Note that the library I used in that example is a work in progress and should not be used as-is in your apps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;re probably used to creating Angular applications, but what about Angular libraries? First, it&#8217;s essential to define what an Angular library is. A library is a collection of components\/pipes\/directives or services bundled together that can be shared and used in multiple code repositories. As a result, Angular libraries are designed to be published on npm [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21],"tags":[],"class_list":["post-1649","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1649"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1649\/revisions"}],"predecessor-version":[{"id":1657,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1649\/revisions\/1657"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}