Introduction
If you've worked with Node.js or JavaScript, you've probably installed packages like:
npm install express
or:
npm install axios
But have you ever wondered:
How are these npm packages created and published?
An npm package is basically reusable code that other developers can install and use in their applications.
For example, instead of writing the same utility functions in every project, you could create a package:
npm install my-utils
and then use:
import { add, subtract } from "my-utils";
In this article, we'll create a simple npm package from scratch, publish it to the npm registry, and use it in another application.
We'll build a small package called:
sundar-number-utils
The package will contain a few reusable number utility functions.
Note: The package name used in this article is only an example. npm package names must be unique, so choose your own available name when publishing.
What Is an NPM Package?
An npm package is a collection of reusable code that can be distributed through the npm registry.
For example:
npm install lodash
downloads the lodash package into your project.
After installation, you can use its functionality:
import _ from "lodash";
Your own package works the same way.
The basic flow is:
Create Package
↓
Write Reusable Code
↓
Configure package.json
↓
Test Package
↓
Login to npm
↓
Publish Package
↓
Install Package
↓
Use Package in Another App
What Is NPM?
npm stands for Node Package Manager.
It provides:
- A package registry
- Package installation
- Dependency management
- Package publishing
- Version management
- Scripts and package configuration
When you run:
npm install express
npm downloads the package from the npm registry and adds it to your project.
You can also publish your own package to the same registry.
What Are We Going to Build?
Let's create a package with three functions:
add()
subtract()
multiply()
For example:
add(10, 20);
returns:
30
Our final package will look something like:
sundar-number-utils
│
├── src/
│ └── index.js
│
├── package.json
├── README.md
└── .gitignore
Step 1: Create a Package Directory
Create a new folder:
mkdir sundar-number-utils
Move into it:
cd sundar-number-utils
Now initialize npm:
npm init
You'll be asked several questions.
You can also use:
npm init -y
This automatically creates a default package.json.
Step 2: Understand package.json
After running:
npm init -y
you'll get something similar to:
{
"name": "sundar-number-utils",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
The package.json file contains important information about your package.
Let's understand the important fields.
Package Name
{
"name": "sundar-number-utils"
}
This is the name users will use when installing your package:
npm install sundar-number-utils
The name must be available on npm.
You can check whether a package name is already taken before publishing.
Version
{
"version": "1.0.0"
}
The version tells users which release of your package they're using.
npm packages commonly follow Semantic Versioning, often written as:
MAJOR.MINOR.PATCH
For example:
1.0.0
A common interpretation is:
1 → Breaking changes
0 → New backward-compatible features
0 → Bug fixes
For example:
1.0.0 → Initial release
1.1.0 → New feature
1.1.1 → Bug fix
2.0.0 → Breaking change
We'll come back to versioning later.
Description
Add a meaningful description:
{
"description": "Simple number utility functions for JavaScript"
}
This helps users understand what your package does.
Main Entry Point
You may see:
{
"main": "index.js"
}
This tells Node.js which file should be treated as the package's main entry point when using the traditional CommonJS package format.
For example:
{
"main": "src/index.js"
}
means the package entry point is:
src/index.js
Modern packages can also use the exports field, which we'll discuss later.
Step 3: Create the Package Code
Create:
src/index.js
Add:
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
module.exports = {
add,
subtract,
multiply,
};
These are the functions our package will provide.
Step 4: Configure the Entry Point
Update package.json:
{
"name": "sundar-number-utils",
"version": "1.0.0",
"description": "Simple number utility functions for JavaScript",
"main": "src/index.js",
"keywords": [
"javascript",
"numbers",
"utilities"
],
"author": "Your Name",
"license": "MIT"
}
Now npm knows that:
src/index.js
is the main entry point.
Step 5: Add a README
A good npm package should have documentation.
Create:
README.md
For example:
# Sundar Number Utils
Simple number utility functions for JavaScript.
## Installation
npm install sundar-number-utils
## Usage
const {
add,
subtract,
multiply
} = require("sundar-number-utils");
console.log(add(10, 20));
console.log(subtract(20, 10));
console.log(multiply(5, 4));
A README is important because npm displays it on your package page.
Step 6: Test the Package Locally
Before publishing, we should test our package.
Create a separate test project:
mkdir test-app
cd test-app
Initialize it:
npm init -y
But our package isn't published yet.
How can we test it?
npm provides a useful command called:
npm link
What Is npm link?
npm link allows you to use your local package inside another local project without publishing it to npm.
First, go into your package:
cd sundar-number-utils
Run:
npm link
Now npm creates a globally linked version of your local package.
Go to your test application:
cd test-app
Then:
npm link sundar-number-utils
Now the test application can use your local package.
Step 7: Use the Package Locally
Inside test-app, create:
index.js
Add:
const {
add,
subtract,
multiply,
} = require("sundar-number-utils");
console.log(add(10, 20));
console.log(subtract(20, 5));
console.log(multiply(5, 4));
Run:
node index.js
Output:
30
15
20
Great!
We've successfully created and consumed our package locally.
Step 8: Login to npm
Now let's publish the package.
First, you need an npm account.
Once you have an account, login from your terminal:
npm login
npm will ask you to authenticate.
After successful authentication, you can verify the logged-in account with:
npm whoami
It should display your npm username.
Step 9: Check the Package Name
Before publishing, make sure your package name is available.
For example:
npm view sundar-number-utils
If the package doesn't exist, npm may return an error indicating that no package was found.
You can also search npm for the name.
Remember that package names must be unique across the npm registry.
Step 10: Check What Will Be Published
Before publishing, it's a good idea to see what files npm will include.
Run:
npm pack --dry-run
This helps you inspect the files that would be included in the package.
You don't want to accidentally publish:
.env
passwords
API keys
large development files
node_modules
For example, if you have:
.env
make sure it isn't included.
Step 11: Add .gitignore
Create:
.gitignore
Add:
node_modules
.env
This is especially important if you're also storing the package in Git.
Remember:
.gitignorecontrols Git, while npm package contents are controlled by npm's packaging rules and fields such asfilesand.npmignore.
Step 12: Publish the Package
Once everything looks good:
npm publish
If publishing succeeds, npm will make the package available through the npm registry.
Your package can then be installed using:
npm install sundar-number-utils
That's the same command developers use for thousands of other npm packages.
Important: Scoped Packages
Sometimes you may want a package associated with your npm username or organization.
For example:
@username/number-utils
A scoped package can be created with:
npm init --scope=@username
Or you can configure the package name directly:
{
"name": "@username/number-utils"
}
For a public scoped package, publishing commonly looks like:
npm publish --access public
This is especially useful when the package name without a scope is already taken.
Step 13: Install Your Published Package
Now let's pretend we're another developer.
Create another application:
mkdir todo-app
cd todo-app
Initialize npm:
npm init -y
Install the package:
npm install sundar-number-utils
npm downloads the package into:
node_modules/
and adds it to package.json.
Step 14: Use the Package
Create:
index.js
Add:
const {
add,
subtract,
multiply,
} = require("sundar-number-utils");
const result1 = add(100, 50);
const result2 = subtract(100, 30);
const result3 = multiply(10, 5);
console.log(result1);
console.log(result2);
console.log(result3);
Run:
node index.js
Output:
150
70
50
That's it.
Your own npm package is now being used in another application.
Using ES Modules
Modern JavaScript projects often use ES modules instead of CommonJS.
We can configure our package to use:
{
"type": "module"
}
Then our package can use:
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
export function multiply(a, b) {
return a * b;
}
The consuming application can use:
import {
add,
subtract,
multiply,
} from "sundar-number-utils";
Which module system you choose depends on your package and the Node.js versions you want to support.
A Better Modern package.json
For a simple modern package, you could use something like:
{
"name": "sundar-number-utils",
"version": "1.0.0",
"description": "Simple number utility functions for JavaScript",
"type": "module",
"main": "./src/index.js",
"exports": {
".": "./src/index.js"
},
"keywords": [
"javascript",
"numbers",
"utilities"
],
"author": "Your Name",
"license": "MIT"
}
The exports field is useful because it explicitly defines which package entry points consumers can access.
For a small package, this is a clean starting point.
What Is the exports Field?
You might see packages using:
{
"exports": {
".": "./src/index.js"
}
}
The "." represents the package root.
So:
import { add } from "sundar-number-utils";
will resolve to:
src/index.js
The exports field also gives package authors more control over which internal files are publicly accessible.
Adding More Functions
Our package currently has only three functions.
We could add:
function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
Then export it:
module.exports = {
add,
subtract,
multiply,
divide,
};
Now consumers can use:
const {
add,
subtract,
multiply,
divide,
} = require("sundar-number-utils");
Versioning Your Package
Suppose you've published:
1.0.0
Now you add a new feature.
You can update the version with:
npm version minor
This changes:
1.0.0
to:
1.1.0
Then publish:
npm publish
For a bug fix:
npm version patch
For a breaking change:
npm version major
This results in:
1.0.0 → 2.0.0
Semantic Versioning
npm packages commonly use Semantic Versioning:
MAJOR.MINOR.PATCH
For example:
1.4.2
means:
1 → MAJOR
4 → MINOR
2 → PATCH
A common approach is:
PATCH
→ Bug fix
MINOR
→ New backward-compatible feature
MAJOR
→ Breaking change
For example:
1.0.0
↓
1.0.1
Bug fix.
1.0.1
↓
1.1.0
New feature.
1.1.0
↓
2.0.0
Breaking change.
Updating a Published Package
Suppose you've already published:
1.0.0
You make some changes.
First:
npm version patch
Now:
1.0.1
Then:
npm publish
Users can update their package with:
npm update sundar-number-utils
Or install a specific version:
npm install sundar-number-utils@1.0.1
What Happens During npm install?
When someone runs:
npm install sundar-number-utils
npm downloads the package from the npm registry.
The package is placed inside:
node_modules/
The dependency is also recorded in package.json:
{
"dependencies": {
"sundar-number-utils": "^1.0.0"
}
}
Now other developers can use the package in their applications.
Adding Tests
If you're publishing a package that other developers will depend on, testing becomes very important.
For example, suppose we have:
function add(a, b) {
return a + b;
}
We should test:
add(2, 3) → 5
add(-2, 2) → 0
add(0, 0) → 0
You can use testing frameworks such as Jest, Vitest, or Node.js's built-in test runner.
A package should ideally have automated tests before publishing production releases.
Adding a Test Script
For example:
{
"scripts": {
"test": "node --test"
}
}
Then:
npm test
can run your tests.
The exact testing setup depends on the package and project requirements.
Adding a Build Step
If you're writing a package in TypeScript or using modern syntax that needs transformation, you may have a build process.
For example:
src/
↓
Build
↓
dist/
Your package might look like:
number-utils/
│
├── src/
│ └── index.js
│
├── dist/
│ └── index.js
│
├── package.json
└── README.md
Then package.json could point to:
{
"main": "dist/index.js"
}
For a simple JavaScript package, however, you don't necessarily need a build step.
Publishing a Scoped Package
Let's say your npm username is:
sundar
You could create:
@sundar/number-utils
Your package.json:
{
"name": "@sundar/number-utils"
}
Then publish publicly:
npm publish --access public
Install it with:
npm install @sundar/number-utils
Use it:
import { add } from "@sundar/number-utils";
Scoped packages are commonly useful for personal namespaces and organizations.
What Should You Avoid Publishing?
Be careful about what goes into your npm package.
Never publish secrets such as:
.env
API keys
Database passwords
Private tokens
Cloud credentials
Also avoid unnecessarily publishing:
node_modules/
temporary files
large development files
private configuration
Before publishing, always inspect the package contents:
npm pack --dry-run
This small step can prevent a serious mistake.
Useful NPM Commands
Here are some commands you'll frequently use when developing packages.
Initialize package
npm init
Initialize with defaults
npm init -y
Install dependency
npm install package-name
Install development dependency
npm install package-name --save-dev
Login
npm login
Check logged-in user
npm whoami
Publish
npm publish
Update version
npm version patch
npm version minor
npm version major
Test package contents
npm pack --dry-run
Create a local package link
npm link
Use the local package
npm link package-name
Complete Package Flow
The complete process looks like this:
Create Folder
↓
npm init
↓
Create Reusable Code
↓
Configure package.json
↓
Create README
↓
Test Locally
↓
npm pack --dry-run
↓
npm login
↓
npm publish
↓
Package Available on npm
↓
npm install package-name
↓
Use Package in Your Application
Real-World Example
Imagine you're building several React or Node.js applications.
You repeatedly write:
formatDate()
or:
validateEmail()
or:
formatCurrency()
Instead of copying these functions between projects, you can create:
my-common-utils
Then every project can install:
npm install my-common-utils
and use:
import {
formatDate,
validateEmail,
formatCurrency
} from "my-common-utils";
Now, when you fix a bug in the utility, you can publish a new version:
npm version patch
npm publish
Your other projects can update to the new version.
This is one of the biggest benefits of creating reusable packages.
Final Thoughts
Creating an npm package might initially look complicated, but the basic process is actually simple.
You need to:
- Create a project.
- Initialize it with npm.
- Write reusable code.
- Configure
package.json. - Add documentation.
- Test the package.
- Login to npm.
- Publish it.
- Install it in another project.
- Maintain it using semantic versioning.
The most important files are usually:
package.json
README.md
src/index.js
And the most important commands to remember are:
npm init
npm login
npm publish
npm version patch
npm version minor
npm version major
Once you understand this workflow, you can start creating packages for your own reusable utilities, components, middleware, API clients, validation libraries, and other developer tools.
The interesting part is that the packages you create don't have to remain personal. If they're useful to others, you can publish them publicly and let developers around the world install them with:
npm install your-package-name
That's essentially how the npm ecosystem is built — developers creating reusable code and sharing it with other developers.