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.
npm --version
It should output the version of npm that you installed.
Now create the package directory using the CLI and do the following:
# make the project directory
mkdir random-number-generator
# cd into the directory
cd random-number-generator
# Initialise the new NPM package in the project directory
npm init
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:
{
"name": "random-number-generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
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:
function randomNumGenerator(min, max) {
if(typeof(max) !== 'number' && typeof(min) !== 'number') {
min = 0; max = 1;
}
return (Math.random() * (max-min)) + min;
}
module.exports = randomNumGenerator;
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.