How to Create an NPM Package: A Step-by-Step Guide
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 example of a simple package that I have created and published on npm — otp generator.
Prerequisites:
Node.js: Ensure that Node.js is installed on your system. You can download it from 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.
- 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 {
let digits = "0123456789";
let OTP = "";
let 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 n digit random 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 project root directory to link your package globally:
npm link
2. Now, create another directory where you will use your package for testing:
mkdir test
cd test
3. 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
4. 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));
5. 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 isindex.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"
}
2. 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 otp which can be used in addition to 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 from the command line:
- Login to NPM:
npm login
You’ll be prompted to enter your username, password, and email associated with your NPM account.
2. 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.
Subscribe to my newsletter
Read articles from Samarth Srivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by