Creating your own NPM (Node Package Manager) module is one of the best ways to contribute to the JavaScript ecosystem, improve your development skills, and build reusable tools for real-world applications. NPM is the largest package registry in the world, and publishing your own module allows other developers to easily install and use your code in their projects.
In this guide, weβll walk through a practical step-by-step process to create, test, and link your own NPM module.
π Why Create an NPM Module?
Before diving into the steps, hereβs why it matters:
- Reuse your code across multiple projects
- Share your utilities with other developers
- Improve modular programming skills
- Gain open-source visibility
- Learn package management deeply
π§± Step 1: Create Project Structure
Create two separate folders:
- Library folder β Your NPM module (core logic)
- Demo folder β To test and use the module
Example:
/my-library
/demo-app
βοΈ Step 2: Initialize NPM in Both Folders
Run the following command in both folders:
npm init -y
This creates a package.json file for both projects.
π¦ Step 3: Setup Library Project
Inside the library folder, install Parcel:
npm install parcel
Create the source file:
src/index.ts
Add your reusable code inside index.ts.
Example:
export function greet(name: string) {
return `Hello, ${name}!`;
}
π§ Step 4: Configure Library Build
Update your package.json:
- Set entry point
- Configure build scripts using Parcel
Example:
"source": "src/index.ts",
"main": "dist/index.js"
π₯οΈ Step 5: Setup Demo Project
Inside the demo folder, install Parcel:
npm install parcel
Create the following structure:
src/
index.html
style.css
app.js
π Step 6: Link Library to Demo
Run this command inside the demo folder:
npm link ../library
This connects your local module to the demo project.
π₯ Step 7: Import Your Module
In app.js, import your library:
import { greet } from "your-library-name";
console.log(greet("Skriptx"));
βΆοΈ Step 8: Run the Projects
Run both projects simultaneously:
In library folder:
npm run watch
In demo folder:
npm start
π Troubleshooting Tip
If changes are not reflected:
- Remove import line
- Re-type the import
- Restart the dev server
π Final Workflow Summary
- Create library & demo folders
- Initialize NPM in both
- Build library using Parcel
- Create demo UI
- Link library using
npm link - Import module in demo
- Run both projects simultaneously
π― Conclusion
Creating your own NPM module is a powerful step toward becoming a professional JavaScript developer. It helps you understand modular architecture, build reusable components, and contribute to the developer ecosystem.
Start small, build utilities, and gradually scale your modules into production-ready packages that others can use.
π Now go ahead and publish your first NPM module to the world!