A component library is one of the coolest things a web developer can make, but if you don't know how to make one, let me guide you a bit. Before we begin I would like to tell you, that I am not a pro at making component libraries, it's just from my experience developing a component library before. Prerequisites Before you get started, these are the following you should know: Basics of React Typescript (Optional but preferred, you can use Javascript too) Why make a component library? A react component library can be useful in many ways, you can use it in your projects as you scale and want uniformity or you can make your library open-source for everyone to use and contribute. Tools Required React - since, this is a tutorial about a react component library Typescript - this is optional but recommended Storybook - it's fine if you haven't heard of it, don't freak out. We will be using this to preview our components Getting Started There are a lot of ways we can set up our library, if you want to make several packages in the same organization I would recommend using a monorepo but for the sake of this tutorial we will be setting up a very simple library from scratch. So, first of all, go ahead and make a file: package.json npm init Once, you have initialized the project, we will start installing our dependencies. Unlike, in a normal project where you would just install the dependencies directly with , here we will be using dev-dependencies and peer-dependencies. npm i [dep-name] Dev-dependencies are the dependencies that do not get packaged in the bundle and do not contribute to the size of our library, it is a dependency that you only require during development. Peer dependencies basically tell you that you need to have a certain package installed to use the library, for example, if I have a library named and it has peer dependencies as & , then if I want to use in my project, I need to have & already installed as dependencies in the project. avi-lib react react-dom avi-lib react react-dom Now go ahead and install , , , & , as devDependencies. react react-dom tyepscript @types/react @types/react-dom yarn i -D react react-dom typescript @types/react @types/react-dom Add & as peer-dependencies with appropriate versions: react react-dom { ... peerDependencies: { "react": ">=16.0.0", "react-dom": ">=16.0.0" } ... } Next, initialize a tsconfig file: npx tsc --init In the file, go ahead and enable with option. tsconfig.json jsx "jsx": "react" Making the Component Once this is done make a directory or whatever you wish to name it. This directory will contain all code. src Inside , make another directory and create a file . Now, open the file and write out a simple button: src components Button.tsx import React from 'react'; export interface ButtonProps { children: React.ReactNode; onClick: () => void; } export const Button = ({ children, onClick }: ButtonProps) => { return <button onClick={onClick}>{children}</button>; }; Now inside the directory, create a file, and write out the following: src index.ts export { Button, ButtonProps } from "./components/Button" Storybook Storybook is an amazing tool that is almost essential for component libraries, it helps you out with previewing your components adding documentation for them, and even lets you host the components for others to test. For the sake of this article, we will be primarily using it for testing our components. To setup storybook in your library, run the following: npx storybook --init Storybook automatically detects that this is a react library and installs it accordingly. After it has done installing you will see a directory and a directory. You don't really have to do anything with the directory, you will use your components in the directory. .storybook src/stories .storybook src/stories Inside the directory you will see a bunch of stuff, but don't worry about it, it is all just mock data. You can go ahead and empty it leaving only the . Now you will see a lot of things in this file go ahead and empty it and type in the following: src/stories Button.stories.tsx import React, { useState } from 'react'; import { storiesOf } from '@storybook/react'; import { Button } from '../'; const stories = storiesOf('Button', module); stories.add('Button', () => { const [value, setValue] = useState('Hello'); const setChange = () => { setValue(value === 'Hello' ? 'Bye' : 'Hello'); }; return <Button onClick={setChange}>{value}</Button>; }); Now go ahead and check it out, run the following in your terminal: npm run storybook This will open a browser window on , and you can see our little button working, its changes on being clicked. localhost:6006 value That's about it, now go ahead and add your own magic to the button, make more components, and similarly, you can even create hooks. Documentation It is a good idea to document things about your components in your library in a README file or on a docs website, it helps out anyone using your library. For the sake of this tutorial, we will not be writing documentation because it only contains a single button component. Bundling Once you are done with making your components, let's bundle our library. It's a good idea to bundle because it helps you produce a single file. Some people prefer dragging and dropping a single file in their projects. A single file can also be used for CDNs. To bundle your component you will have to use a bundler. There are a lot of options out there but here we will be using because tsup doesn't require a configuration for our use case. tsup To install tsup, do the following: yarn add -D tsup This installs tsup as a dev-dependency, now inside your file add a script: package.json { ... scripts: { ... "build": "tsup src/index.ts --dts" } ... } Now, go ahead and run , this will create a file inside the directory. If you observe, we added a in our build script, this will generate the type definitions for our project in . yarn build index.js dist --dts index.d.ts Publishing Now that all is done, it's time to show what you have built to the world (or not if it's for personal use). Before publishing, go to your and you will find a few things, choose a name for your library and add an appropriate version (you can use if you don't know how to), a description of what it does, and a few keywords to make your library discoverable. package.json semantic versioning Now add a file, this is very similar to , this file will contain what you don't want to publish along with your library, for example, . Though this file is optional even if you don't include it, the file will be used instead. .npmignore .gitignore node_modules .gitignore If you are not logged in to your npm account do and you will have to enter your username and password. In the case, you don't have an npm account you can go to and register for one. npm login https://www.npmjs.com/ Once you have done that just simply run this command in the terminal: npm publish Once it finishes without any errors, Congrats! Your library has been published. Now you (and others too) can use your library in your projects. Tips Try to use typescript Add documentation in your README & if you want, a docs website I have not done it in the article but use git and make changes on a different branch than main Read more on Storybook, its templates, and documentation More Resources - in case you wish to create a monorepo, you can also use instead of turborepo. Turborepo lerna Storybook Documentation Hope you enjoyed the read and learned something from it. In the case, you want to add something or find a mistake and wish to point it out, please feel free to do so in the comments. Also published . here