Step-by-Step guide to make your first react package
If you ever made an application with react, then you must have used lots of reactjs packages. Ever thought about making your own package? or Have an idea but don't know how? Then this blog is for you. In this blog, I will explain how to make your own react package and submit it to the npm registry.
- npm account
Let's get started
Step-1 - Initialize the project
⚠️ - Don't create project with
- Create a project ->
- Now we have to make sure the user have these installed, so we can add them as peer dependencies in
- Create a
srcfolder and add an
index.jsfile. Inside that
- Our project folder structure so far-
Step-2 - Setup project
Now we need to see our components while we are building it, so how we can do it as we are not creating a regular react app? For this, we can use a tool called Storybook.
- To install the storybook, simply run this command -
⚠️ - Make sure you have storybook CLI installed in order to run this command. To install storybook CLI, run
npm i @storybook/cli -g
- Now you can see in the
src/storiesfolder it created some example stories. You can delete them.
Step-3 - Start creating stories
- Create a component in
src/componentsfolder. For example,
- Now create a story in
src/storiesand name it as
<Component>.stories.js. For example,
- Import your component into
- In order to see our component we have added our component to stories. To do that, in your
So this is the syntax to create a story.
- Now start your storybook server-
- Go to localhost:6006, you can see your rendered component there.
- Like this, you can create a story for every component and see a preview with the storybook.
Step 4 - Prepare to build the project
Normally we create a build for our project after developing with
npm run build. But now we can't do that. So to build the project we have to use another tool called rollup.js along with some plugins.
- Install rollup as a dev dependency
- We also need some plugins for rollup and to remove react modules in our package(Because users will already have them installed).
- Create a file called
rollup.config.jsat the root level of the project.
- And you can add this configuration-
input - starting pointing of your project
output - where your want to put all the build files
plugins - plugins to use
- Now create a script to run rollup
- That's it, now you can see a folder called dist which contains all our code bundled together.
Step 5 - Publish to npm
- Create an account on [npm] if you don't have it already.
- Connect to npm with
- Choose a unique name for your package. (When publishing the
name of the package will be the same as the name of your project which
is in the
- You need to make two changes to your
- Change main from "index.js" to "dist/index.js"
- Add the module field and set it to "dist/index.es.js"
- After completing all things, run-
- That's it, you can check your package on the npm registry
Congrats on publishing your first react package 🎉.
Share your package in the comments below. I will try it 👀.
⚒️Tool of the week⚒️
Front-End-Checklist Are you a front-end developer? Then go through this checklist before deploying your application.
LEAVE A COMMENT OR START A DISCUSSION
5 min read
Hiring Angular Developers in 2023: Insider Tips & Strategies
Does your company struggle to create a modern, high-performance, cross-platform web app? Do you desire cost-effective development without compromising future software quality? Many web development teams utilize Angular to build complicated single-page apps. Hiring Angular programmers is difficult.
8 min read
How Feature Flags Can Help You Ship Faster and Smarter?
Are you tired of long development cycles and hesitant to push new features to production? Feature flags may be the solution you're looking for. In this blog post, we'll explore how feature flags can streamline your development process, reduce the risk of errors, and give you more control over the features you release to your users. From testing new features to rolling out changes to a select group of users, feature flags can help you do it all. Keep reading to learn how you can start using feature flags in your development workflow today.