×
  • Best Way To Build And Publish Angular Library (v10)

  • 06 – Publish Angular Library To Public NPM Repository

    Lesson List

    In this final lesson, we will see how can we Publish Angular Library To Public NPM Repository which we created with the npm pack command.

    Signup NPM Repository

    The first thing will do is we have to signup for an account with npmjs for publishing the angular library to the pubic npm repository.

    You can create your npmjs account here: https://www.npmjs.com/signup

    Provide your credentials by running the below mention command from the terminal.

    npm adduser

    (Note: This is a one-time operation only and you can skip this step if you have already logged in.)
    Once you are logged in, we have to navigate to the dist folder of the application and then inside the library folder where we have created the news-twentyfour-0.0.1.tgz file using npm pack command.

    Publish Angular Library

    Note: To publish it to the public library the .tgz file must be there.

    publish library using below command:

    npm publish - -access public

    Then we can see the published package on npm at this URL:
    https://www.npmjs.com/package/< library- name>
    Now as we have created and published our library to the public NPM repository we can install it anywhere in angular projects.

    npm install — save <library-name>@0.0.1

    (here 0.0.1 is the version number of a library, which is necessary)


    Few Adjustments:

    As a good programming practice, we should have some unique names for the library we create.
    Because our npm’s public registry will sure have many packages with these similar names. So you can follow the approach to add your npm’s username with your library name so that it can be uniquely identified and also to avoid the errors while you publish it.

    –library_path–/package.json
    
    {
      "name": "@__username__/news-twentyfour",
      "version": "0.0.1",
      "peerDependencies": {
        "@angular/common": "^10.1.1",
        "@angular/core": "^10.1.1"
      },
      "dependencies": {
        "tslib": "^2.0.0"
      }
    }
    
    Publish Angular Library To Public NPM Repository
    Publish Angular Library To Public NPM Repository
    .
    .
  • Enable Notifications    Ok No thanks