Creating and publishing your own NPM (Node Package Manager) package can be a great way to share useful code, collaborate with the open-source community, and contribute to the JavaScript ecosystem. Here’s a step-by-step guide to help you build, publish, and manage your own NPM package. I will be using the example of a simple package that I have created and published on the npm — otp generator.
Prerequisites
- Node.js: Ensure that Node.js is installed on your system. You can download it from the Node.js official site.
- NPM account: Sign up on NPM if you haven’t already.
Step 1. Initialize a New Project
First, you need to create a new directory for your package and initialize it as an NPM project.
1. Create a project directory
mkdir otp-generator
cd otp-generator
2. Initialize NPM: Run the following command and answer the prompts (you can also skip this and directly modify the package.json later).
npm init
This will generate a package.json file that contains metadata about your package, such as the package name, version, description, and entry point (usually index.js).
Step 2. Write your Package Code
Create the main file for your package. Typically, the entry point is index.js, though it can be any file name you specify in the main field of package.json.
For example, create an index.js file.
touch index.js
Then, write the functionality for your package inside index.js. Here’s a simple example.
function getOTP(length) {
try {
const digits = "0123456789";
let OTP = "";
const len = digits.length;
for (let i = 0; i < length; i++) {
OTP += digits[Math.floor(Math.random() * len)];
}
return OTP;
} catch (err) {
throw err;
}
}
module.exports = getOTP;
In this case, you’ve created a basic package that exports a function getOTP, which takes a number/length as input and returns a random digit number, which then can be used as otp.
Step 3. Test Your Package Locally
Before publishing your package, it’s a good idea to test it locally.
Run the following command in the project root directory to link your package globally.
npm link
Now, create another directory where you will use your package for testing.
mkdir test
cd test
Inside the test directory, link the package you created.
npm link otp-generator
Note. The name of the package will be the one mentioned in package.json
Create a test file (test.js), and require your package to ensure everything works as expected.
const getOTP = require('otp-generator');
console.log(getOTP(6));
Run the test.
node test.js
If everything is working, you should see a random number like 825765
Step 4. Prepare for Publishing
Now that your package works locally, it’s time to prepare it for publishing.
Update package.json: Open the package.json file and ensure that all relevant fields are correctly filled out. The most important fields are.
- name: The name of your package (must be unique on NPM).
- version: Follow semantic versioning (e.g., 1.0.0).
- description: A brief explanation of what your package does.
- main: The entry point file (default is index.js).
Here’s an example package.json.
{
"name": "otp-generator",
"version": "1.0.0",
"description": "Generates random OTP for the given length",
"main": "index.js",
"keywords": [
"otp",
"one time password"
],
"author": "Samarth Srivastava",
"license": "ISC"
}
Add a README file: Write a README.md file to document your package. This should include installation instructions, usage examples, and any other relevant information.
Example README.md
# OTP Generator
This package generates random OTPs, which can be used with any messaging service or as a random number generator for the given length.
## Usage/Examples
```javascript
const getOTP = require('otp-generator');
console.log(getOTP(6)); // Returns a 6-digit random number
## ๐ Links
[![portfolio](https://img.shields.io/badge/my_portfolio-000?style=for-the-badge&logo=ko-fi&logoColor=white)](https://github.com/Samarth-Srivastava)
[![linkedin](https://img.shields.io/badge/linkedin-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/samarthsrivastava/)
Step 5. Publish Your Package
Before you can publish your package, you need to log in to your NPM account using the command line.
Login to NPM
npm login
You’ll be prompted to enter the username, password, and email associated with your NPM account.
Publish the package: Once logged in, publish your package by running.
npm publish
If everything is set up correctly, your package will be live on the NPM registry.