How to build and publish an NPM package for React using Typescript?
Last Updated :
24 Apr, 2025
In development, creating and distributing reusable components is essential for building scalable and maintainable applications. With the popularity of TypeScript and React, you can easily package and share your components as NPM packages. This tutorial will teach us how to create and release our NPM packages (NPM modules).
Prerequisites:
What is NPM?
NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integrated into Node projects to extend their functionality.
NPM packages offer numerous advantages, a few of which are outlined below:
- Reusable code
- Code management (with versioning)
- distributing code
What is Typescript
TypeScript is a strict superset of JavaScript, which means anything that is implemented in JavaScript can be implemented using TypeScript along with the choice of adding enhanced features. It is an Open Source Object Oriented programming language and strongly typed language.
Steps to build NPM Package:-
Step 1: Open the GitHub account and create new empty repository and give the name stringToLower then click on create repository button.
create repositoryStep 2:- Then go to the code editor(VS Code) and Open the terminal and create new empty folder and then open it.
mkdir stringToLower
Step 3: Open this folder terminal and initialize the git repository
git init
git remote add origin https://github.com/YourRepo.git
git remote -v

Step 4:-Go to code editor, create README.md file in the folder and open terminal and check the status of the git repository
git status
Step 5:- write something your Readme file and push into the GitHub repository by using the following command.
git add .
git commit -m 'Initial'
git push -u origin master
write readme file and push in GitHub repositoryStep 6:- initialize the project using this command :- npm init -y and then go to package.json file and give the keywords and the author name and change licencse type ISC to MIT.

Step 7:- Create a file .gitignore and add node_modules in it.
.gitignore fileStep 8: Install typescript using the following command and Create a folder src and inside it create a file index.ts
npm install -D typescript
install typescriptStep 9:- For the configuration of TypeScript create a file tsconfig.json. then give some congiguration code like this:
JavaScript
{
"compilerOptions": {
"target": "ES5",
"module": "CommonJS",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules","**/__tests__/*"]
}
Step 10:- Add the following code in src/index.ts file
JavaScript
//src/index.ts
export class StringtoLowercase{
static ToLower(stringName:string) {
let lower = "";
for (var i = 0; i < stringName.length; i++) {
var value = stringName.charCodeAt(i);
if (value >= 65 && value <= 90) {
lower += String.fromCharCode(value + 32);
}
else {
lower += stringName[i];
}
}
console.log(lower);
}
}
Step 11:- Modify the package.json file.
1. In script object change the "test" to "build" and give the value "tsc".
build successfully and create file and folder2. Save the file and build the project with the following command:
npm run build
3. It will create a lib folder and inside the lib folder two file like index.d.ts and index.js will be created.

4. Go to .gitignore file and add lib folder which which will not be pushed to GitHub repository.
add lib folder5. Go to package.json file and add the file object.
add file object6. Update the entry point of file and types.
change main and add types object and add prepare key in Script object7. Check the git status (using this command: "git status") and push this code in GitHub repository by following the step5.
Steps to Publish NPM Package:-
Step 1:- Create the NPM account and go to profile and click on package button(If there is any package it will show otherwise no package found message will show )
go to profileStep 2:- Then go to vs code terminal and login with npm using the following command.
npm login
login npmStep 3: Check that you are login successfully or not using the following command.
npm whoami
Step 4: In the terminal, write the command to publish your npm package
npm publish
publish successfullyStep 5: Go to npm.com and go to packages you will find your published package.
published
Similar Reads
How to Build a Simple and Scalable Web API using Nest.js and Typescript ?
NestJS is a progressive Node.js framework that leverages TypeScript and is built on top of Express.js. Itâs designed to provide an application architecture out of the box, which helps to create highly testable, maintainable, and scalable applications. In this guide, we will walk through the steps to
5 min read
How to execute TypeScript file using command line?
TypeScript is a statically-typed superset of JavaScript that adds optional type annotations and compiles to plain JavaScript. It helps catch errors during development. To execute a TypeScript file from the command line, compile it using tsc filename.ts, then run the output JavaScript file with node.
2 min read
How to use TypeScript to build Node.js API with Express ?
TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will
4 min read
Steps to Create and Publish NPM packages
In this article, we will learn how to develop and publish your own npm package (also called an NPM module). There are many benefits of NPM packages, some of them are listed below: Reusable codeManaging code (using versioning)Sharing code The life-cycle of an npm package takes place like below: Modu
7 min read
Build an Online Code Compiler using React.js and Node.js
In this article, we will learn how to build an online code compiler using React.js as frontend and Express.js as backend. Users will be able to write C, C++, Python, and Java code with proper syntax highlighting as well as compile and execute it online. The main objective of building an online compi
7 min read
How to redirect in React with Typescript ?
Navigating users seamlessly through a React application is a fundamental aspect of creating a smooth and intuitive user experience. In this article, we delve into the world of redirects in React, specifically addressing the use of TypeScript for enhanced type safety and developer productivity.Prereq
2 min read
Top npm Packages for React
ReactJS, often referred to as React is a popular JavaScript library developed by Facebook for building user interfaces. It emphasizes a component-based architecture, where UIs are built using reusable components. It has a vast range of libraries which helps it to become more powerful. In this articl
4 min read
How to make Mongo schema from pure Typescript classes ?
TypeScript is an object-oriented programming language, and it is a superset of JavaScript and contains all of its elements. By using TSC (TypeScript Compiler), we can convert Typescript code (.ts file) to JavaScript (.js file). It is open-source and its code is easier to read and understand. MongoD
8 min read
Blog Page Template using React JS and Tailwind
A Blog Page is a web page that is used to display multiple blog posts on a website. Using blogs people can share their views, ideas, and opinions. A Blog Page generally contains a NavBar and Introduction followed by multiple blogs displayed in the form of a card. Prerequisites:React JSTailwind CSS p
6 min read
How to publish a ReactJS component to NPM ?
Follow these simple steps in order to publish your own ReactJS component to NPM. Step 1: Initial Setup In order to publish any ReactJS Component to npm (node package manager), first we have to create a React component in the React app. Following are the instructions for creating any react app. Creat
3 min read