This website uses cookies to ensure you get the best experience on our website.

Skip to content
LOGO
  • Company

    About IGNEK

    Learn about our story and our mission.

    Careers

    We're hiring!

    We always looking for talented peoples. Join our team!

    Blogs

    The latest industry news, updates and info.

    Case Studies

    Take a closer look at the projects we've delivered

    Webinar

    Watch our latest organized webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Custom Enterprise Portal Development for flawless Solutions

    Dedicated Development Team

    Expand Teams, Optimize Development with Our Expertise

    Back-end Development

    Empowering your digital presence with robust backend development expertise

    Front-end Development

    Crafting engaging user experiences through expert frontend development

    Digital Transformation

    Embrace digital transformation by modernizing process

    SaaS Development

    Transform your business with SaaS Innovative Solutions

    Technologies

    Liferay Services

    Development & Customization

    Theme

    Upgradation

    Migration

    Proof of Concept

    Performance Tunning

    Architecture

    Support & Maintenance

    E-commerce

    Expert Advice

    React Services

    Enterprise Development

    Custom Application

    Single Page Application

    Architecture

    API Integration

    Migration

    Consulting

    Maintenance & Support

    Spring Boot Services

    Web Service

    MVC Web Development

    API Integration

    Security

    Migration

    Microservices

    Support & Maintenance

    AEM Services

    Consulting Service

    Site Development

    Migration & Upgradation

    Integration

    Maintenance & Support

    Custom Solutions

    Content Management

    Team Augementation

  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Transforming industries through cutting edge SaaS solutions.

    Customer Portal : Boost Business Productivity

    Streamline operations and enhance productivity with our Customer Portal solution.

    Liferay Based Intranet Portal

    Internal portal for company communication, collaboration and resources

    Integration

    Matomo Integration with Liferay

    Integrate Matomo to enable user interactions seamlessly.

    Microsoft Teams integration with Liferay

    Integrate to enable seamless collaboration, messaging within your portal.

    Jira Integration With Liferay

    Optimizes business operations by integrating with Jira.

  • Hire Us

    Liferay

    Get expert Liferay developers for seamless portal solutions.

    React JS

    Get expert ReactJS developers for dynamic web solutions.

    Web Developer

    Get custom web solutions from IGNEK's expert developers.

    Spring Boot

    Get top-notch Spring Boot developers for your project success!

    Healthcare

    Get top Healthcare Developers for cutting-edge medical software.

    MERN Stack

    Get expert MERN developers from IGNEK for custom, innovative project solutions.

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • info@ignek.com
    • Ahmedabad, Gujarat, India – 382470
SCHEDULE CALL
  • COMPANY
    • About
    • Case Studies
    • Blogs
    • Career
    • Webinar
  • SERVICES
    • Enterprise Portal Development
    • Dedicated Development Team
    • Back-end Development
    • Front-end Development
    • Digital Transformation
    • SaaS Development
  • SOLUTION
    • Customer Portal:  Boost Business Productivity
    • SaaS : Software as a Service
    • Liferay Based Employee Intranet Portal
  • TECHNOLOGY
    • Liferay Services
      • Liferay Development and Customization
      • Liferay Theme Development Service
      • Liferay DXP 7.4 Upgrade
      • Liferay Migration
      • Liferay Proof Of Concept
      • Liferay Performance Tuning
      • Liferay Architecture Design Service
      • Liferay Support And Maintenance Service
      • Liferay Ecommerce Development
      • Liferay Expert Advice
    • ReactJS Services
      • ReactJS Enterprise Application Development
      • ReactJS Custom Application Development
      • ReactJS Single Page Application Development (SPA)
      • ReactJS Applications Architecture
      • ReactJS API Integration
      • ReactJS Migration
      • ReactJS Consulting
      • ReactJS Maintenance and Support
    • Spring Boot Services
      • Spring Boot Web Development Service
      • Spring MVC Web Development
      • Spring Boot API Integration Service
      • Spring Boot Security
      • Spring Boot Migration Service
      • Spring Boot Microservices
      • Spring Boot Support & Maintenance Service
    • AEM Development Services
      • AEM Consulting Services
      • AEM Site Development
      • AEM Migration & Upgradation
      • AEM Integration Services
      • AEM Maintenance & Support
      • AEM Content Management
      • Custom AEM Solutions
      • AEM Team Augmentation
  • INTEGRATION
  • HIRE US
    • Hire Liferay Developer
    • Hire ReactJS Developer
    • Hire Spring Boot Developer
    • Hire Healthcare Developer
    • Hire Web Developer
    • Hire MERN Stack Developer
  • CONTACT US
  • Company

    About IGNEK

    Careers

    We're hiring!

    Blogs

    Case Studies

    Webinar

    Get in touch with our HR team

    • hr@ignek.com
    • +91 93284 95160
    • Ahmedabad, Gujarat, India – 382470
  • Services

    Enterprise Portal Development

    Dedicated Development Team

    Back-end Development

    Front-end Development

    Digital Transformation

    SaaS Development

    Technologies

    Liferay Liferay Services
    • Development & Customization
    • Theme
    • Upgradation
    • Migration
    • Proof of Concept
    • Performance Tuning
    • Architecture
    • Support & Maintenance
    • E-commerce
    • Expert Advice
    React React Services
    • Enterprise Development
    • Custom Application
    • Single Page Application
    • Architecture
    • API Integration
    • Migration
    • Consulting
    • Maintenance & Support
    Spring Boot Spring Boot Services
    • Web Service
    • MVC Web Development
    • API Integration
    • Security
    • Migration
    • Microservices
    • Support & Maintenance
    AEM AEM Services
    • Consulting Service
    • Site Development
    • Migration & Upgradation
    • Integration
    • Maintenance & Support
    • Custom Solutions
    • Content Management
    • Team Augmentation
  • Solution

    Ready for digital excellence

    In our rapidly evolving world, businesses are always on the lookout for fresh ways to improve their operations and connect with their audiences.

    Contact US

    Solutions

    SaaS : Software As A Service

    Customer Portal : Boost Business Productivity

    Liferay Based Intranet Portal

    Integration

    Matomo Integration with Liferay

    Microsoft Teams integration with Liferay

    Jira Integration With Liferay

  • Hire Us

    Liferay

    React JS

    Web Developer

    Spring Boot

    Healthcare

    MERN Stack

    Why Hire Developers From IGNEK ?

    • Certified Developer
    • Transparent Communication
    • Flexible Hiring Modals
    • Fully Signed NDA
    • Cost Effective
    • Easy Exit Policy

    Get in touch with our Sales team

    • sales@ignek.com
    • +91 6351576580
    • IGNEK
    • Ahmedabad, Gujarat, India – 382470
  • SCHEDULE CALL

Turn Your Code Into a Package: Step-by-Step NPM Publishing Guide

  • ReactJS
  • July 4, 2025

Share On :

Why Publish an NPM Package? Use Cases & Benefits

If you’ve written the same bit of code more than once — like a helper function or a small React component — you might as well just package it. Saves time, keeps things tidy, and you won’t have to copy-paste all over the place.

Publishing to NPM isn’t some huge thing either. Once you go through it once, it’s just another step in the workflow. Build, version, push. Done. Also handy if someone else needs to use the same logic.

This guide covers how to structure code, build an NPM package, and publish it — with a focus on JavaScript and React.

When to Create a Package
  • Reusable Utility Functions : You’ve got a few utility functions — stuff like capitalizeWords, formatDate, whatever — and you keep using them in every project. No point copying them over and over. Package them up once, reuse them cleanly.
  • Shared UI Components : Built a Button, Modal, or some form thing in React? And now you’re using the same one across multiple apps? Probably a good time to move it into a shared package.
  • Organization-Wide Standards : Your organization has internal coding standards (e.g., logging wrappers, error handlers, custom hooks).
  • Open Source Contribution : You’ve built something you believe others can benefit from.
When Not to Create a Package
  • Code is too specific
    If the code you wrote depends directly on your app’s database or local config or even how your UI state is managed, don’t make it a package. It’s too tied to one setup.
  • It’s too early
    Just because you wrote a couple of functions doesn’t mean it needs to be packaged. If it’s not reused yet, just leave it in the project.
  • Trying to move a full app
    Putting your whole codebase or all app logic into a shared package? Not a good idea. Packages should be for smaller, reusable stuff.
Benefits of Publishing an NPM Package
  • Use code in more than one place
    Components, utilities, hooks — whatever it is, if you need it in multiple projects, a package makes it easier.
  • Track changes
    Semantic versioning helps keep updates clear. You know what changed and when.
  • Work better with your team
    Everyone uses the same shared code. Keeps things consistent and avoids rewriting.
  • Share with others
    Publishing helps others who have the same problem. You might get feedback or improvements too.
  • Keeps things clean
    Moving logic into its own package helps avoid clutter in your main project.
Pitfalls to Watch Out For
  • Bad or missing docs
    If your README doesn’t explain how to use the package, don’t expect others (or even your future self) to know what to do with it.
  • Publishing just for the sake of it
    Not everything needs to be on NPM. If it only works in your project, maybe leave it there.
  • Leaking sensitive stuff
    Be extra careful not to push anything like secrets, keys, or internal logic that shouldn’t be public.
  • Extra work after release
    Once it’s out there, you’re on the hook. People might open issues, ask for fixes, or expect updates. That’s part of the deal.
  • Package size problems
    If you don’t trim things down, you might ship a big bundle. That slows down apps that depend on it. Keep it lean.
How to Create an NPM Package

Step 1 : Initialize Your Project

  • Create a new folder for your package :
				
					mkdir format-utils
cd format-utils

				
			
  • Initialize a package.json file :
				
					npm init
				
			

During setup, provide :

  1. Package name: Ensure it’s unique (e.g., format-utils or @yourname/format-utils)
  2. Version: Start with 1.0.0
  3. Entry point: index.js
  4. License: MIT or your choice
  5. Description and keywords : Important for discoverability
package

Step 2 : Write Utility Functions

Create an index.js file with practical formatting utilities :

				
					
// Capitalizes the first letter of each word in a given string
function capitalizeWords(str) {

    // Ensure the input is a string
    if (typeof str !== 'string') {
        throw new TypeError(`capitalizeWords(): Expected a string, got ${typeof str}`);
    }

    // Use regex to find word boundaries and capitalize the first character of each word
    return str.replace(/\b\w/g, char => char.toUpperCase());
}

// Converts a camelCase string to a Title Case string (e.g., 'myExampleString'  =>  'My Example String')
function camelToTitle(str) {

    // Ensure the input is a string
    if (typeof str !== 'string') {
        throw new TypeError(`camelToTitle(): Expected a string, got ${typeof str}`);
    }

    // Insert a space before each uppercase letter
    const spaced = str.replace(/([A-Z])/g, ' $1');

    // Trim leading/trailing spaces and capitalize the first letter of each word
    return capitalizeWords(spaced.trim());
}

// Formats a given Date object or date string into the format 'dd/mm/yyyy'
function formatDate(date) {

    // Create a Date object from the input
    const d = new Date(date);
    // Ensure input is a valid Date or string that can be converted to a Date  
    if (!(date instanceof Date) && typeof date !== 'string') {
        throw new TypeError(`formatDate(): Expected a Date object or date string, got ${typeof date}`);
    }

    // Check if the created Date is valid
    if (isNaN(d.getTime())) {
        throw new Error(`formatDate(): Invalid date provided`);
    }

    // Extract and pad day, month, and year
    const day = String(d.getDate()).padStart(2, '0');
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const year = d.getFullYear();

    // Return formatted date
    return `${day}/${month}/${year}`;
}

// Export the utility functions
module.exports = {
    capitalizeWords,
    camelToTitle,
    formatDate,
};
				
			

Step 3: Add a README File

Create a README.md explaining :

  • What the package does
  • Installation instructions
  • Code examples

Example :

				
					# format-data-utils

A lightweight JavaScript utility library for formatting strings and dates with runtime validation.

## Installation

```bash
npm install format-data-utils

				
			
How to Publish Your NPM Package

Step 1 :  Run the following and enter your credentials

				
					npm login
				
			

If you don’t have an account, create one at npmjs.com.

Step 2 : Publish Your Package to the NPM Registry

Make sure your package.json is correctly filled.

If using a scoped name (@yourname/format-utils), publish with public access :

				
					npm publish --access public

Or just run 

npm publish

				
			

You can now view it on: https://www.npmjs.com/package/format-data-utils

How to Install and Use the Package

Step 1 : Install the Package in Any Project

From NPM :

				
					npm install format-data-utils
				
			

Or from GitHub :

				
					npm install git+https://github.com/yourusername/yourpackagename.git
				
			
install

Step 2 : Import and Use the Functions

				
					const { capitalizeWords, camelToTitle, formatDate } = require('format-utils');

console.log(capitalizeWords('hello world')); // Hello World
console.log(camelToTitle('firstName'));      // First Name
console.log(formatDate('2025-06-23'));       // 23/06/2025

				
			
output1

Invalid usage throws helpful errors :

				
					capitalizeWords(123);     // TypeError
formatDate('bad-date');   // Error: Invalid date

				
			
output2
output3
Bonus Tips & Best Practices
  • Use semantic versioning
    Follow the usual version pattern: bug fixes = patch (1.0.1), new features = minor (1.1.0), breaking stuff = major (2.0.0). Don’t skip this — it helps anyone using your package.

  • Pick a name that’s not taken
    Before you publish, search npmjs.com. If your name’s already in use, tweak it. Add something specific like -js, or use scoped names — for example, @yourname/tool-name.

  • Add some tests
    Doesn’t have to be full test coverage. A few basic checks are better than nothing. Helps catch mistakes before someone else finds them.

  • Build your code if needed
    If you’re using JSX or new JS features, make sure to transpile first. Babel, Rollup, Vite — any of those work. Just don’t ship raw modern code unless you’re sure it’ll run everywhere.

Conclusion

You don’t need anything fancy to publish a useful NPM package. If you’ve got code you use often — maybe a function, maybe a small React component — turning it into a package can save time. You write it once and reuse it wherever.

Just make sure it’s easy to set up and works on its own. Add some docs. Keep the versioning clean. That’s it. It doesn’t have to be perfect — just something that does one job well.

Explore Our Services

Discover how we can help your business thrive, whether you’re running a small startup, an SME, or a large enterprise. We’re here to understand your unique needs and goals, offering the expertise and resources to support your journey to success.
Stay informed about our ReactJS services and updates by subscribing to our newsletter—just fill in the details below to subscribe.

Loading
Loading...

Related Blogs

March 27, 2024
ReactJS Performance Tuning
July 19, 2024
Simplifying State with Context API
Simplifying State with Context API
August 30, 2024
Simplifying Global State in React with useContext and useReduce
Simplifying Global State in React with useContext and useReducer
Loading...

Featured Projects Portfolios

November 27, 2023
Government Job Platform User-Friendly, Secure and Scalable
Government Job Platform : User-Friendly, Secure, and Scalable
October 4, 2023
Advanced SaaS Customer Portal_ License Management, SSO, and Customer Support
Advanced SaaS Customer Portal : License Management, SSO, and Customer Support
October 4, 2023
Telemedicine Appointment & Health Record Portal
Telemedicine Appointment & Health Record Portal

Digital Solutions for Your Business with IGNEK

4.9

5.0

5.0

5.0

LOGO

Making the world a better place through constructing elegant hierarchies

COMPANY

  • About
  • Career
  • Case Study
  • Blogs

SERVICES

  • Enterprise Portal Development
  • Dedicated Development Team
  • Back-end Development
  • Front-end Development
  • Digital Transformation
  • SaaS Development

HIRE US

  • Liferay
  • Spring Boot
  • ReactJS
  • Healthcare
  • MERN Stack
  • AEM

CONTACT

  • info@ignek.com
  • info@ignek.com
SALES
  • sales@ignek.com
  • (+91) 635 157 6580
CAREER
  • hr@ignek.com
  • (+91) 932 849 5160
  • E 910-912, Ganesh Glory 11, Jagatpur Road, SG Highway, Ahmedabad, Gujarat - 382470

© 2018-2025 IGNEK, Inc. All rights reserved

Linkedin Facebook X-twitter Instagram