How to write and publish your first NPM package
Rajesh Kumaravel
Posted on October 19, 2020
Before we begin…
Before we begin this, you should have the following things set up.
- GitHub account
- NodeJS and npm installed
- npm account
Choosing a package name
You need to check whether the name is available for the npm package. If you are publishing unscoped (public) package; the name of the package should be unique.
However, if you are publishing a scoped (private) package then name does not have to be unique and name takes the format of @npm_username/package-name
Read more on
Initializing the npm package
- Create a new directory and initialize using
npm init
$ npm init
Initializing package will ask you a few questions for setup.
The default package.json
file will be created in your project directory
{
"name": "number-to-comma-format",
"version": "1.0.0",
"description": "Convert a number to comma separated format",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "RAJESH K",
"license": "MIT"
}
Read more on package.json
Create the Node module
The main
field in package.json defines the entry point of your package.
Let's create index.js
file
$ touch index.js
Add the following code to index.js
'use strict'
const defaultOptions = {
minimumFractionDigits: 2,
maximumFractionDigits: 2
};
/**
* @param {Number} num - Number to be converted
* @returns - Formatted number format
*/
function formatNumberToComma(num) {
return Number(num).toLocaleString('en', defaultOptions);
}
module.exports = formatNumberToComma;
Add a README
It's a good idea to include documentation for your package so others know how to use it
Generally, a README should cover
- Description of what your package does
- Installation and usage instructions
- Example code
- Contribution guidelines
- License used by the package Choosing the right license
Test your package locally
It is recommended to test your package locally before publishing to npm
- Create and initialize new project outside package directory
- Package can be installed by the following command
npm install number-to-comma-format
The problem with this is that your package is still not published yet so it isn't in npm. You need to reference your package locally while developing and testing it.
You could install the package using an absolute path to the package.
npm install /home/rajesh/dev/number-to-comma-format
Initialize Git
Create a new repository in github for your package and push the source code to git
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/rajeshkumaravel/numbertocommaformat.git
git push origin master
Publish the package to npm
- Sign in to npm
- You'll be prompted to enter username, password and email address which was used while registering to npm
npm login
- Now you can publish your package to npm by
npm publish
Wrapping Up
VOILA...! Your package is now published on npm.
- Initialize
npm init
- Add node module/source code
- Test your package locally
- git initialize
- Publish package
npm publish
And that’s it!
I hope you found this article a useful primer for getting started with publishing on npm, and as always, thanks for reading!
Check this out npm package for more reference Node express request id Source
Happy Coding!
RK
Posted on October 19, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.