This post is about 'How to Publish Your Own Public NPM Package?' To demonstrate, I will create one package to convert Nepali number to English number and English number to Nepali number. Finally, I will publish it to npmjs.com.
December 1, 2022 ┬╖ 8 min ┬╖ 1624 words ┬╖ Prakash Bhandari
Table of Contents
Publishing your own public NPM package is not that hard.
All you need is basic understanding of Javascript, NPM, package.json file and Account in https://www.npmjs.com
In this post, I will try to explain the steps to create and publish your own public NPM package to https://www.npmjs.com.
To demonstrate, I will create real NPM package which will convert English number to Nepali Number and Nepali number to English Number.
Finally, this package will be publish to https://www.npmjs.com for public use.
I assume you already have installed the Node in your machine.
If you don’t have node in your machine please install form Node.js.
NPM comes with NodeJS, NPM gets installed on your machine automatically when you install Node in your machine.
Let’s create project folder for our package.
mkdir nepali-english-number-converter
cd nepali-english-number-converter
Now, you are in root of the project. Next step is to initiate the project by creating package.json file.
package.json file gets automatically created when you run npm init command in your project root directory.
Let’s create files and folders as defined in below Files and Folders Structure for the project.
I will also write unit test by using one of most popular JavaScript framework called Jest.
I have installed Jest for this. But, you can totally ignore this step, because you might feel boring :)
The test code for each file inside the __tests__/ should look like this.
constenglishToNepali=require("../src/english-to-nepali");test("english 123 number should converted to резреирей",()=>{expect(englishToNepali(123)).toBe("резреирей");});test("english 12345 number should converted to резреи,рейрекрел",()=>{expect(englishToNepali(12345)).toBe("резреи,рейрекрел");});test("english 12345.05 number should converted to резреи,рейрекрел.режрел",()=>{expect(englishToNepali(12345.05)).toBe("резреи,рейрекрел.режрел");});test("english 12345.05 number should converted to резреи,рейрекрел.режрел",()=>{expect(englishToNepali(12345.05)).toBe("резреи,рейрекрел.режрел");});test("english 123456789.05 number should converted to резреи,рейрек,релрем,ренреореп.режрел",()=>{expect(englishToNepali(123456789.05)).toBe("резреи,рейрек,релрем,ренреореп.режрел");});
constconvert=require("../src/index");test("english резреирей number should converted to 123",()=>{expect(convert("резреирей","TO-EN")).toBe(123);});test("english резреирей number should converted to 123 with to-En.",()=>{expect(convert("резреирей","to-En")).toBe(123);});test("english резреирей number should converted to 123 with invalid type. Invalid type should return false",()=>{expect(convert("резреирей","t1o-En")).toBe(false);});test("english резреи,рейрекрел number should converted to 12345",()=>{expect(convert("резреи,рейрекрел","to-en")).toBe(12345);});test("english резреи,рейрекрел.режрел number should converted to 12345.05",()=>{expect(convert("резреи,рейрекрел.режрел","TO-EN")).toBe(12345.05);});test("english резреи,рейрек,релрем,ренреореп.режрел number should converted to 123456789.05",()=>{expect(convert("резреи,рейрек,релрем,ренреореп.режрел","tO-eN")).toBe(123456789.05);});// english to nepali test
test("english 123 number should converted to резреирей",()=>{expect(convert(123,"TO-NP")).toBe("резреирей");});test("english 123 number should converted to резреирей",()=>{expect(convert(123,"to-Np")).toBe("резреирей");});test("english 123 number should converted to резреирей with invalid type. Invalid type should return false",()=>{expect(convert("резреирей","to-en1")).toBe(false);});test("english 12345 number should converted to резреи,рейрекрел",()=>{expect(convert(12345,"to-np")).toBe("резреи,рейрекрел");});test("english 12345.05 number should converted to резреи,рейрекрел.режрел",()=>{expect(convert(12345.05,"to-np")).toBe("резреи,рейрекрел.режрел");});test("english 12345.05 number should converted to резреи,рейрекрел.режрел",()=>{expect(convert(12345.05,"To-nP")).toBe("резреи,рейрекрел.режрел");});test("english 123456789.05 number should converted to резреи,рейрек,релрем,ренреореп.режрел",()=>{expect(convert(123456789.05,"tO-np")).toBe("резреи,рейрек,релрем,ренреореп.режрел");});
constenglishToNepali=require("../src/nepali-to-english");test("english резреирей number should converted to 123",()=>{expect(englishToNepali("резреирей")).toBe(123);});test("english резреи,рейрекрел number should converted to 12345",()=>{expect(englishToNepali("резреи,рейрекрел")).toBe(12345);});test("english резреи,рейрекрел.режрел number should converted to 12345.05",()=>{expect(englishToNepali("резреи,рейрекрел.режрел")).toBe(12345.05);});test("english резреи,рейрек,релрем,ренреореп.режрел number should converted to 123456789.05",()=>{expect(englishToNepali("резреи,рейрек,релрем,ренреореп.режрел")).toBe(123456789.05);});
constenglishToNepali=require("./english-to-nepali");constneplaiToEnglish=require("./nepali-to-english");module.exports=function(number,type){if(typeoftype==="undefined")returnfalse;if(typeoftype!=="string")returnfalse;//convert type in upper case
type=type.toUpperCase();if(!["TO-EN","TO-NP"].includes(type))returnfalse;if(type==="TO-EN"){returnneplaiToEnglish(number);}if(type==="TO-NP"){returnenglishToNepali(number);}};
Sometimes you need to also create a .npmignore file in your project root folder.
It acts the same way as .gitignore does. To ignore the files and folder that you don’t want to publish.
For our module we don’t need to create.
All npm packages contain a file, usually in the project root, called package.json -
this file holds various metadata relevant to the project. This file is used to
give information to npm that allows it to identify the project as well as handle
the project’s dependencies.
A package.json file will be automatically created when you run npm init command in the root of the project.
As mention above, this file holds various metadata like name, version, description, repository url , authors , keywords ect.
A package.json file must contain “name” and “version” fields.
The “name” field contains your package’s name, and must be lowercase and one word, and may contain hyphens and underscores.
{"name":"nepali-english-number-converter","version":"1.0.2","description":"English to Nepali and Nepali to English number converter","main":"./src/index.js","repository":"https://github.com/dev-scripts/nepali-english-number-converter.git","scripts":{"test":"jest"},"author":{"name":"Prakash Bhandari","email":"thebhandariprakash@gmail.com","url":"https://www.prakashbhandari.com.np"},"keywords":["Nepali Number converter","English to Nepali number converter","Nepali to English number converter","Hindu Arabic to English number","English number to Hindu Arabic","Number Converter","NPM package converter English number to Nepali number"],"bugs":{"url":"https://github.com/dev-scripts/nepali-english-number-converter/issues"},"homepage":"https://github.com/dev-scripts/nepali-english-number-converter#readme","license":"MIT","dependencies":{"jest":"^29.3.1","save-dev":"0.0.1-security"}}
If you don’t have account on npmjs.com. You have to create an account on https://www.npmjs.com.
If you have your account on npmjs.com you can publish your module under your account.