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.

What is NPM?

NPM is a package manager for Node.js packages, or modules.

www.npmjs.com acts like git for Node.js packages and hosts millions of public packages which can be downloaded and used in your project.

What is a Package?

According to w3schools

“A package in Node.js contains all the files you need for a module.”

“Modules are JavaScript libraries you can include in your project.”

NPM Project Setup

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.

Files and Folders Structure for the project

My file and folder structure will be like below in my project. I will be creating all the files and folders as I move further.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
|- __tests__/
| | english-to-nepali.test.js
| | index.test.js
| | nepali-to-english.test.js
|-src/
| | english-to-nepali.js
| | nepali-to-english.js
| .gitignore
| LICENSE
| package-lock.json
| package.json
| README.md

Create files inside __tests__/ folder

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.

  1. english-to-nepali.test.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const englishToNepali = 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("резреи,рейрек,релрем,ренреореп.режрел");
});
  1. index.test.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const convert = 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("резреи,рейрек,релрем,ренреореп.режрел");
});
  1. nepali-to-english.test.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const englishToNepali = 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);
});

Create files inside src/ folder

The fines inside src/ folder should look like this.

  1. english-to-nepali.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const englishToNepaliNumber = {
    0: "реж",
    1: "рез",
    2: "реи",
    3: "рей",
    4: "рек",
    5: "рел",
    6: "рем",
    7: "рен",
    8: "рео",
    9: "реп",
};
const englishToNepali = (englishNumber) => {
    englishNumber = englishNumber.toString().replace(/,/g, "");
    englishNumber = new Intl.NumberFormat("en-IN").format(englishNumber);

    const arrayEnglishNumber = englishNumber
        .toString()
        .split("")
        .map((character) => {
            if (character === "." || character === ",") {
                return character;
            }

            return englishToNepaliNumber[Number(character)];
        });

    return arrayEnglishNumber.join("");
};

module.exports = englishToNepali;
  1. index.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const englishToNepali = require("./english-to-nepali");
const neplaiToEnglish = require("./nepali-to-english");

module.exports = function (number, type) {
  if (typeof type === "undefined") return false;

  if (typeof type !== "string") return false;

  //convert type in upper case
  type = type.toUpperCase();

  if (!["TO-EN", "TO-NP"].includes(type)) return false;

  if (type === "TO-EN") {
    return neplaiToEnglish(number);
  }

  if (type === "TO-NP") {
    return englishToNepali(number);
  }
};
  1. nepali-to-english.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const nepaliToEnglishNumber = {
    "реж": 0,
    "рез": 1,
    "реи": 2,
    "рей": 3,
    "рек": 4,
    "рел": 5,
    "рем": 6,
    "рен": 7,
    "рео": 8,
    "реп": 9,
};

const neplaiToEnglish = (nepaliNumber) => {
    nepaliNumber = nepaliNumber.toString().replace(/,/g, "");
    const arrayEnglishNumber = nepaliNumber
        .toString()
        .split("")
        .map((character) => {
            if (character === ".") {
                return character;
            }

            return nepaliToEnglishNumber[character];
        });

    return Number(arrayEnglishNumber.join(""));
};

module.exports = neplaiToEnglish;

Create .gitignore file

You can place all the files and folder that you don’t want to commit to git repo.

1
node_modules

Create .npmignore file

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.

Create LICENSE file

You can copy and modify my licence text, or you can copy form https://opensource.org/licenses/MIT

Create package.json file

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.

The “version” field must be in the form x.x.x .

If you want to know more about the package.json file please visit: Creating a package.json file

Here, I have attached my package.json file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
  "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"
  }
}

Create README.md file.

Create README.md file, and you can write the document on how to use the package.

Create Account on npmjs.com

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.

Login into npmjs.com

Run npm login command in the root of the project. You have to provide the login details from console.

Publish module to npmjs.com

Run npm publish inside your project root directory

Once you publish to into npmjs.com you are done ЁЯН╗ and your package is publicly available to user by the community.

How to use publicly available NPM package by others people

Now this module is publicly available on npmjs.com

Publish URL: https://www.npmjs.com/package/nepali-english-number-converter

Install using npm

1
npm install nepali-english-number-converter --save

Or if you prefer using yarn

1
yarn add nepali-english-number-converter --save

Example of package implementation.

1
2
const convert = require("neplai-number-converter")
console.log(convert(123, 'TO-NP'));

Output : резреирей

After completing this post we have learned how to create a basic NPM package and publish to the npmjs.com for public use.

Full source code form GitHub Repository : https://github.com/dev-scripts/nepali-english-number-converter

Publish URL : https://www.npmjs.com/package/nepali-english-number-converter

YouTube Video:

References

  1. https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry
  2. https://nodejs.org/en/download/
  3. https://www.w3schools.com/nodejs/nodejs_npm.asp