Back

Thursday, 21 November 2024 | 17:19 | Author by Asep Saepul Rohmat

Creating NPM Packages with Custom CLI

What is a Package Manager

A Package Manager is a tool for managing dependencies or packages required by an application or project. This tool takes these packages from repositories, both local and public, and installs them on the system.

In programming, we rarely start a project from scratch. Usually, we integrate several existing libraries or packages that are created by others. This can happen because we may not be able to create the library, or we just want to focus on product development. In the end, we save time by utilizing libraries that have been shared by others. Using what has been created by others can speed up the development process and avoid the effort of "reinventing something that already exists."

With a package manager, application development becomes easier and more efficient, because developers do not need to download and install packages manually. This package contains reusable code and provides various features and functionality to manage our project dependencies such as libraries, frameworks, and utilities.

 

Steps to Create an NPM Package

To create an NPM package, follow these steps.

1. Install Node.js

Visit the official Node.js website and download the LTS version or the latest version according to your needs.

Once downloaded, follow the installation instructions that suit your operating system (Windows, macOS, or Linux).

2. Initialize the Project

Create a project directory for your package manager:


Initialize the project using npm init:


Then update the package.json file


3. Install Commander.js for CLI

Package managers like NPM use the Command Line Interface (CLI). You can use the Commander library for custom CLI.

Install Commander:


4. Create a program file

Create a src folder and a JavaScript file in it, for example index.js:


Add code to the index.js file to create a simple command.



5. Test the npm package that has been created

Create a local package link


In the project where the package will be installed, add the following command


Check the package after installation


If it has been successfully installed, you can start using the package that has been prepared

command 1


Output:


command 2


Output:


6. Publish

First, you must have an NPM account to publish your npm package.

Enter the following command

 

Congratulations, your NPM Package has been successfully published and you can install it via NPM.

 



Congratulations, your NPM Package has been successfully published and you can install it via NPM.

 

Conclusion

By following the steps above, you can create your package manager that works like NPM, from Node.js installation to implementing features such as dependency management, caching, and version management. This opens up great opportunities for you to customize and explore various NPM packages according to your project needs. In addition, with this capability, you can create a package ecosystem that suits project specifications and deepen your understanding of how package managers work.

For other interesting information about technology and IT, visit: Radya Blog. And for consultation in creating applications, you can contact our team at: Radya Contact.

 

Source:

How to Create and Publish an NPM Package — a Step-by-Step Guide

How To Create An NPM Package


Add Comment


Related Blog