December 5, 2020

hosein

Articles - Tutorials

How to create a NPM module

NPM is the most popular package manager for javascript which let’s you install and import many useful modules into your projects.

In this post we will learn how to create our own node module step by step and publish them on npm so that everyone can use them, this will also be really useful if you have created a useful module for one of your projects and you want to be able to install and use it in any project.

Create project folder and init npm

first of all create a folder for your package and run this command in it:

npm init

now the terminal will ask you to enter some information about your package like name, version, description, entry point file and git repository. enter them and press enter to initialize your package and create a package.json file, containing information about the package.

Create entry point file

create a file in the folder root and name it the name that you entered as entry point file name while initializing the package in our case we create a index.js file.

so now that we have an entry point we can start writing code for our package. for example I want to write a function to format numbers with thousands separators.

but in order to make this function importable in other files we have to export it with “module.exports”

index.js:

module.exports = function (x) {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};

if you want to add multiple methods you have to create functions this way:

module.exports.functionName:

module.exports.formatNumber = function (x) {
  // function code hare
};

module.exports.formatString = function (x) {
  // function code hare
};

to keep this tutorial simple and quick (as you probably want!) I don’t add any more methods or files to this package so we move on to publish our number formatter package.

Publish your package

so now taht our package is ready let’s publish it to npm, first of all login to npm in the terminal:

npm login

and enter your username and password to login, if you don’t have an account, create a new npm account with npm adduser

now that we have logged in to npm we can publish our package by running this command:

npm publish

notice that the package name should be unique if it isn’t change the package name in package.json file.

after publishing your package you can find it in npm by searching it’s name in npmjs.com. if you want your package page have a full description or instructions or something, put a readme.md file in the package folder.

Using your new package

now that we have published our package we are ready to use it and test it, so create a new folder init the project and install the new package that you just created:

npm install num-formatter --save

create an index.js file and import and test the package:

test/index.js:

const formatNumber = require('num-formatter')

console.log(formatNumber(455000000))

and after running the code with “node index.js” we will see that our package is working as expected:

node index.js
// 455,000,000

I hope this tutorial have been helpful, now you are able to create useful libraries to use in your javascript projects.