Creating and Publishing your first npm package quickly

Creating and Publishing your first npm package quickly

Introduction

This article will take you through all the series of steps involved for an npm package to get published and become a part of the official npm repository for everyone to use. These are majorly the following steps:

  • Create the npm package locally.
  • If you don't have it already, create an NPM registry account; otherwise, login to your existing account.
  • Publish your npm package to the npm registry.
  • We are managing package versioning.
  • You are testing your published package using npm install.
  • Creating the npm package

    Since the article demonstrates and covers the steps that One should take to get an npm package published in the official npm registry, we will keep the package basic and straightforward so that we can focus on publishing the package. All packages' creating and publishing cycles are nearly the same; therefore, once one understands how it's done for something as simple as what we will be building in this article, you are good to go to publish your package.

    Hence, we’ll create a simple package for generating random numbers using our module.

    Prerequisite

    All one needs to create(and then publish) an NPM package is the NPM command-line tool that goes by the name npm, i.e. 'Node Package Manager. It is distributed inherently with NodeJS, i.e. when one installs NodeJS, they automatically get npm installed. Visit here to download NodeJS.

    To verify whether one installed npm properly, one can run.

    Not supported

    It should output the version of npm that you installed.

    Now create the package directory using the CLI and do the following:

    Not supported

    The npm init command should lead one to the interactive session in the CLI that will be required to be answered on some questions, which are basically about the NPM package that one is creating. The command-line tool auto-suggests reasonable answers to most questions; if one feels the suggested answer not being good enough, one can skip the question by hitting enter. If you also don't have an answer to any of the questions, hit enter and continue; you can always edit it later.

    After answering the questions, one will create a package.json file in the root of the project directory; the content will be similar to this:

    Not supported

    The Package.json file is the single and most important file as far as creating/publishing NPM packages is concerned; without it, one won't be able to publish their packages to the NPM registry.

    Note that the main field in the package.json file refers to the file's name that would be loaded when another application requires the package; by default, it points to 'index.js'.

    Creating the package that one would publish

    The project that we are creating here is a simple one; all entry points to the utilities of any project, in most cases, would be composed in a single file. One can also create something more complex here.

    For this package, create an index.js file in the root of the project directory and add the function for the random number generator:

    Not supported

    Notice that the module. Exports at the bottom of the file; whatever you are exporting here is what would be available for importing when others install the package.

    Creating or logging in to an npm registry account

    Authenticate

    Before publishing their packages to the official npm registry/repository, one needs to have an existing npm registry account. If you don't have one already (since this is your first package, you wouldn't), you can do this here.

    To set up an account, click here.

  • Click on the Sign-Up button. https://lh5.googleusercontent.com/po6Cg5gLPomefjjf1QPfuwrbT1Vxb7pBQ1o1T3Gq_6FlNiu6FWvNxqF7fASJqg-9wSyH_871QG3XX31_bFbPGHNf547vogeFBwu-_-LJ8DV2kIk6-GWBwyTL6EiYjJsK_k-oruho
  • Choose and enter a username, email address and password.
  • Note: The username must be unique. Be sure to keep track of your credentials; you will need them to publish. You will also receive an email to confirm your account.

    https://lh6.googleusercontent.com/aBBM2jTvjNd3ir84PTPrjAjrE6H1XFglGHln5C9ROa4PLUjbq7WykQ8SdqklZhIQGACaSuDbJhEQjvUCNvMIKzUrtoAPpudSjJCd4OSMPEyIeLN7CHm_g9pFRG2LIj5iJQYQNgqD

    If you have trouble creating an account, you can follow the documentation here.

    After creating an account, go back to the command line and authenticate yourself with the command npm login,

    Not supported

    You would be prompted to provide your details, provide the required information and hit enter.

    To test that the login was successful, enter the command npm whoami; one should log your username to the CLI.

    Publish your npm package to the npm registry

    Publish

    Make final edits to your package.json before publishing.

    Note: Remember that the package's name should be unique in the NPM registry. Change it if necessary. You can check that here.

    You add the previously skipped details, such as your npm registries, such as the keywords and author information.

    Not supported

    Now you can publish your package using,

    Not supported

    On publishing, one should see the following notices in the Terminal and get a confirmation email to check if one successfully published the package.

    Not supported

    Package Versioning

    The version value of the package here is a combination of 3 parts separated by a dot operator. Let's say the version is x.y.z

  • Here, the first value (x in x.y.z) specifies the prior version of the package — Which means this version has Major code changes and it might contain breaking API changes.
  • While the second value (y in x.y.z) specifies the minor version, which contains minor changes but will not contain breaking API changes.
  • The third value (z in x.y.z) specifies the patch version, usually containing bug fixes.
  • In our case, you just added a readme.MD file is not an API change, so you can increment the patch version, which is the last part, by 1.

    So change the version inside the package.json file from 1.0.0 to 1.0.1 and run the npm publish command again.

    Testing package installation

    To test the package, you need to install and use it. You can do the following to test the random-number-generator package:

    Not supported

    Use the module by importing it into your test file or directly copy this simple test snippet and paste it into the test.js file.

    Not supported

    Now run the above snippet in the test.js file using node ./test.js; you should be able to see a random number logged to the console.

    FAQs

  • What is the sequence of steps that one needs to follow to publish their npm package to the official npm registry?
  • To publish your npm package, one can take the following steps:

  • Create and test your npm package locally.
  • If you don't have it already, create an NPM registry account; otherwise, log in to your existing account.
  • Now, publish your npm package to the npm registry.
  • Test your published package using npm install.
  • 2. What do the different parts in a package version denote?

    The version value of the package here is a combination of 3 parts separated by a dot operator. The first value denotes the preliminary version of the package; the second value specifies the minor version change. In contrast, the third value indicates the patch version or minor bug fixes in the package.

    Key Takeaways

    In this article, we took a deep dive into the steps required to create and publish a simple NPM package so that anyone can install and use it. Once you have published the package, you can refactor the code or add functionality. If you do this, change the version number in the package.json before Publishing.

    Do Like The Post if it helped!!!