How to Create and Publish Your Own NPM Package

1. Introduction In this guide, I’ll walk you through the entire process of creating and publishing your first NPM package — from setting up your project, writing the code, and testing it locally, to pushing it live on the NPM registry. Whether you’re building a small utility or planning a larger open-source project, this article will give you a solid starting point. Creating your own NPM package allows you to: Reuse your code across multiple projects. Share useful tools with the dev community. Build a portfolio and get recognised for your work. 2. Prerequisites Basic knowledge of JavaScript/TypeScript. Node.js and npm installed. An npm account. 3. Setting Up Your Project mkdir my-awesome-package cd my-awesome-package npm init -y 4. Writing Your Package Code Create an index.js or index.ts file. my-awesome-package/ ├── src/ │ └── index.js (or index.ts) ├── package.json └── README.md /** * Capitalizes the first letter of a string * @param {string} str - The string to capitalize * @returns {string} The capitalized string */ function capitalize(str) { if (typeof str !== 'string') { throw new TypeError('Expected a string'); } return str.charAt(0).toUpperCase() + str.slice(1); } module.exports = capitalize; { "name": "my-awesome-package", "version": "1.0.0", "main": "src/index.js", ... } 5. Testing Locally How to test your package locally in another project using npm link. Before publishing your package to the world, you’ll want to test it in a real-world environment — like another project that uses your package. That’s where npm link comes in. cd ~/projects/my-awesome-package npm link This makes your local package globally available (as if it was published). You’ll see a message like: /Users/you/.nvm/versions/node/v18.0.0/lib/node_modules/my-awesome-package -> /Users/you/projects/my-awesome-package go to the test project: cd ~/projects/test-app npm link my-awesome-package In your test project, you can now import your package like this: // test-app/index.js const capitalize = require('my-awesome-package'); console.log(capitalize("linked!")); // Output: "Linked!" 6. Adding README.md Your code may be brilliant — but if no one knows what it does or how to use it, it won’t go far. That’s why a clear, concise, and helpful README.md is essential. Here’s a simple but effective layout for your README.md: My Awesome Package `Short description

Apr 21, 2025 - 20:22
 0
How to Create and Publish Your Own NPM Package

npm package image

1. Introduction

In this guide, I’ll walk you through the entire process of creating and publishing your first NPM package — from setting up your project, writing the code, and testing it locally, to pushing it live on the NPM registry. Whether you’re building a small utility or planning a larger open-source project, this article will give you a solid starting point.

Creating your own NPM package allows you to:

  1. Reuse your code across multiple projects.
  2. Share useful tools with the dev community.
  3. Build a portfolio and get recognised for your work.

2. Prerequisites

Basic knowledge of JavaScript/TypeScript.
Node.js and npm installed.
An npm account.

3. Setting Up Your Project

mkdir my-awesome-package
cd my-awesome-package
npm init -y

4. Writing Your Package Code

Create an index.js or index.ts file.
my-awesome-package/
├── src/
│ └── index.js (or index.ts)
├── package.json
└── README.md

/**
 * Capitalizes the first letter of a string
 * @param {string} str - The string to capitalize
 * @returns {string} The capitalized string
 */

function capitalize(str) {
  if (typeof str !== 'string') {
    throw new TypeError('Expected a string');
  }

  return str.charAt(0).toUpperCase() + str.slice(1);
}

module.exports = capitalize;
{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "main": "src/index.js",
  ...
}

5. Testing Locally

How to test your package locally in another project using npm link.
Before publishing your package to the world, you’ll want to test it in a real-world environment — like another project that uses your package.
That’s where npm link comes in.

cd ~/projects/my-awesome-package
npm link

This makes your local package globally available (as if it was published).

You’ll see a message like:

/Users/you/.nvm/versions/node/v18.0.0/lib/node_modules/my-awesome-package -> /Users/you/projects/my-awesome-package
go to the test project:

cd ~/projects/test-app
npm link my-awesome-package

In your test project, you can now import your package like this:

// test-app/index.js

const capitalize = require('my-awesome-package');

console.log(capitalize("linked!")); // Output: "Linked!"

6. Adding README.md

Your code may be brilliant — but if no one knows what it does or how to use it, it won’t go far.
That’s why a clear, concise, and helpful README.md is essential.
Here’s a simple but effective layout for your README.md:

My Awesome Package

`Short description