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:
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.
It should output the version of npm that you installed.
Now create the package directory using the CLI and do the following:
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:
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:
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.
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.
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.
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,
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
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.
Now you can publish your package using,
On publishing, one should see the following notices in the Terminal and get a confirmation email to check if one successfully published the package.
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
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.
To test the package, you need to install and use it. You can do the following to test the random-number-generator package:
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.
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.
To publish your npm package, one can take the following steps:
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.
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!!!