<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Madhu Naik</title>
    <description>The latest articles on DEV Community by Madhu Naik (@madhunaik92).</description>
    <link>https://dev.to/madhunaik92</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F910270%2F9a7c0bd5-04b6-4818-b132-c10e2a164f02.jpg</url>
      <title>DEV Community: Madhu Naik</title>
      <link>https://dev.to/madhunaik92</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/madhunaik92"/>
    <language>en</language>
    <item>
      <title>How to publish Angular npm package.</title>
      <dc:creator>Madhu Naik</dc:creator>
      <pubDate>Wed, 17 Aug 2022 10:29:46 +0000</pubDate>
      <link>https://dev.to/madhunaik92/how-to-publish-angular-npm-package-m96</link>
      <guid>https://dev.to/madhunaik92/how-to-publish-angular-npm-package-m96</guid>
      <description>&lt;p&gt;Angular packages are used to share the same functionality to multiple angular application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Below command will create empty workspace. while selecting the package name always select some unique and meaning full name.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ng new package_name --create-application=false


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Navigate to project folder using below command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cd package_name


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then generate library using below command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ng generate library package_name


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will create library files inside project folder&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyfmhsngxi7z2qac22l9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyfmhsngxi7z2qac22l9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;for example if you want build package of custom directive or custom pipe then create custom directive using ng generate command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ng g directive name_of_the_directive
// for custom pipe
ng g pipe name_of_the_pipe


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Write all you you logic inside pipe or directive, and also we have mention the file in App.module.ts&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F606skpudwo0jizmn19gz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F606skpudwo0jizmn19gz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And also we have to import the newly added file in the public-api.ts file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26oldamgdmrel6ipjsef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26oldamgdmrel6ipjsef.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Build package
&lt;/h2&gt;

&lt;p&gt;Building the package is similar to angular application build process.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ng build --prod


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;this will create the dist folder inside your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test your package locally
&lt;/h2&gt;

&lt;p&gt;we can use npm link to test our package before going to publish.&lt;/p&gt;

&lt;p&gt;below are the step to link you package to angular application &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build the application using &lt;code&gt;ng build --prod&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Go to dist/package_name folder using the command line.&lt;/li&gt;
&lt;li&gt;Use  &lt;code&gt;npm link&lt;/code&gt;  This allows you to reference package_name locally.&lt;/li&gt;
&lt;li&gt;Go to you Angular Application where you want to install the package run the below command.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm link package_name 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;this will create the symbolic link between the the package_name and your angular Application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In angular.json file inside architect -&amp;gt; build -&amp;gt; option  add the this &lt;code&gt;"preserveSymlinks": true&lt;/code&gt;  this will keep the link between package and application alive.&lt;/li&gt;
&lt;li&gt;Then import your package module inside you Angular application app.module.ts and test you package.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;import {NgxNumberonlyDirectiveModule} from 'ngx-numberonly-directive'&lt;/p&gt;

&lt;p&gt;@NgModule({&lt;br&gt;
    declarations: [&lt;br&gt;
    ],&lt;br&gt;
    imports: [&lt;br&gt;
      NgxNumberonlyDirectiveModule,&lt;br&gt;
    ],&lt;br&gt;
    providers: [ ],&lt;br&gt;
    bootstrap: [AppComponent]&lt;br&gt;
  })&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Publish your package&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;below are the step to publish your npm package.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build the application using &lt;code&gt;ng build --prod&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Go to dist/package_name folder using the command line.&lt;/li&gt;
&lt;li&gt;You have to login into your npm account using   &lt;code&gt;npm login&lt;/code&gt; .&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;npm publish&lt;/code&gt; to publish your npm package.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Write package information and usage guidelines.
&lt;/h2&gt;

&lt;p&gt;It is very import to write information of the package and usage and installation guidelines. yo have to write all you usage guidelines inside &lt;code&gt;README.md&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnlx6ywc7q2v1bu26slr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwnlx6ywc7q2v1bu26slr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every time before publishing the package please change the version inside package.json file other wise it will throw error. Also you can Add keyword and license related to your package inside package.json.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc7bq84d4gevd8m3rz1e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc7bq84d4gevd8m3rz1e.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>npm</category>
      <category>angular</category>
      <category>angularpackage</category>
      <category>angularlibrary</category>
    </item>
  </channel>
</rss>
