Steps to Create Your Own NPM Module

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

  1. Create library & demo folders
  2. Initialize NPM in both
  3. Build library using Parcel
  4. Create demo UI
  5. Link library using npm link
  6. Import module in demo
  7. 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!