How to publish a ReactJS component to NPM ?
Last Updated :
25 Nov, 2022
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.
- Create a React application using the following command:
npx create-react-app foldername
- After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 2: Creating the Component
Open your project folder in your code editor you will see many folders, depending upon your choice make a folder called components, like in our case we had made the folder inside the src folder as shown below.

Then go to your created folder and create your ReactJs Component, like in your case we have created a Navbar component using react-bootstrap called Header.js.
Header.js
import React from "react";
import {
Nav,
Navbar,
NavDropdown,
Form,
FormControl,
Button,
} from "react-bootstrap";
const Header = () => {
return (
<>
<Navbar bg="light" expand="lg">
<Navbar.Brand href="#home">
ReactNavbarComponent
</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="ml-auto">
<Nav.Link href="#home">Home</Nav.Link>
<Nav.Link href="#link">Link</Nav.Link>
<NavDropdown title="Dropdown"
id="basic-nav-dropdown">
<NavDropdown.Item href="#action/3.1">
Action
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.2">
Another action
</NavDropdown.Item>
<NavDropdown.Item href="#action/3.3">
Something
</NavDropdown.Item>
<NavDropdown.Divider />
<NavDropdown.Item href="#action/3.4">
Separated link
</NavDropdown.Item>
</NavDropdown>
</Nav>
<Form inline>
<FormControl type="text"
placeholder="Search"
className="mr-sm-2" />
<Button variant="outline-success">
Search
</Button>
</Form>
</Navbar.Collapse>
</Navbar>
</>
);
};
export default Header;
Step 3: Environment Setup for Publishing
Go to the components folder using your terminal & type npm init. You will see the following things in your command line.
Use `npm install <pkg>` afterward to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (components) react-navbar
version: (1.0.0)
description: React-navbar
entry point: (Header.js) # Entry Point publishing
test command:
git repository:
keywords:
author: <Name>
license: (ISC)
About to write to C:\Users\Desktop\Tcw\app\src\components\package.json:
{
"name": "react-navbar",
"version": "1.0.0",
"description": "React-navbar",
"main": "Header.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "<Name>",
"license": "ISC"
}
Is this OK? (yes)
Install all the dependencies needed for your component as peer dependencies. For this first, you have to go to your terminal and type the following command:Â
npm install (your dependencies name)
For our example, we have to install these two dependencies for our component using the following command:
npm install react
npm install react-bootstrap
Now go to the package.json file & change the dependencies as peer dependencies and delete the node_module folder from the components folder.
Now if you write npm install your terminal you will see the following instructions:
npm WARN [email protected] requires a peer of react@^17.0.2 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of react-bootstrap@^1.6.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No repository field.
up to date in 3.59s
found 0 vulnerabilities
Step 4: Publishing to npm
Go to the npm website and create an account using your mail ID and password. Make sure that you are in the current directory that is the components directory and type the following commands:
npm login
# Use your username and password what you have created in the npm website
# Then run this command
npm publish

Now your ReactJs component has been successfully published into the npm. Go to your npm account and there you can see your package as shown below.
Similar Reads
How to use Portal Component in ReactJS ?
The portal component renders its children into a new subtree outside the current DOM hierarchy. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Portal Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReact JSMate
2 min read
How to use Badge Component in ReactJS?
Badge generates a small badge to the top-right of its children component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Badge Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReact JSMaterial UISteps to Create
2 min read
How to use NoSsr Component in ReactJS?
NoSsr purposely removes components from the subject of Server Side Rendering (SSR). Material UI for React has this component available for us, and it is very easy to integrate. We can use the NoSsr Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReactJSMaterial UISteps
2 min read
How to use Rating Component in ReactJS ?
Rating Component helps capture the user feedback in form of rating. With the help of this feature, users can also rate the products they have purchased. Material UI for React has this component available for us, and it is very easy to integrate. We can use the following approach in ReactJS to use th
2 min read
How to use Link Component in React JS?
Material-UI provides a set of components that can be seamlessly integrated with React to create visually appealing and functional navigation elements. The Link component allows you to easily customize anchor elements with our own theme colors and typography styles. Material UI for React has this com
2 min read
How to put ReactJS component inside HTML string ?
Putting the React JS component inside the HTML library is not recommended and may lead to conflict in React lifecycle methods. But it can be done by parsing the HTML String into DOM using the html-to-react module. html-to-react is a lightweight library that is responsible for converting raw HTML to
3 min read
How to use Typography Component in ReactJS?
Effectively present your design and content with clarity using typography. With Material UI for React, integration is a breeze. Utilize the Typography Component in ReactJS effortlessly by following this straightforward approach. Prerequisites:Node JS or NPMReact JSMaterial UISteps to create React Ap
2 min read
How to update the State of a component in ReactJS ?
To display the latest or updated information, and content on the UI after any User interaction or data fetch from the API, we have to update the state of the component. This change in the state makes the UI re-render with the updated content. Prerequisites: NPM & Node.jsReact JSReact StateReact
3 min read
How to create components in ReactJS ?
Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to write ReactJS Code in Codepen.IO ?
Now everything is online, some people use VScode to write react.js code and face most of the difficulty. The VScode requires setting for writing React.js code and Many beginners faced difficulty to use VScode so, for them, it is good and easy to use codepen. The codepen provide you with an online pl
2 min read