27
Guide to make your first react package
If you ever made an application with react, then you must have used lots of react 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.
⚠️ - Don't create project with
create-react-app
npm init
react
and react-dom
as devDependencies
npm i --save-dev react react-dom
package.json
.
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
src
folder and add an index.js
file. Inside that src
folder create components
folder.react-pack/
├── src/
│ ├── components/
│ └── index.js
└── package.json
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.

⚠️ - Make sure you have storybook CLI installed in order to run this command. To install storybook CLI, run
npm i @storybook/cli -g
npx sb init
src/stories
folder it created some example
stories. You can delete them.
react-pack/
├── src/
│ ├── components/
│ ├── stories/
│ └── index.js
└── package.json
src/components
folder. For example, Button.jsx
.src/stories
and name it as <Component>.stories.js
. For example, Button.stories.js
<Component>.stories.js
.
react-pack/
├── src/
│ ├── components/
│ │ └── Button.jsx
│ ├── stories/
│ │ └── Button.stories.jsx
│ └── index.js
└── package.json
<Component>.stories.js
-
import React from "react";
import { storiesOf } from "@storybook/react";
import Button from "../components/Button";
// create story
const stories = storiesOf("Button", module);
// add component to stories
stories.add("Button", () => <Button />);
So this is the syntax to create a story.
npm run storybook

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.npm install rollup --save-dev
npm install rollup @rollup/plugin-node-resolve rollup-plugin-babel rollup-plugin-peer-deps-external rollup-plugin-postcss rollup-plugin-terser @babel/preset-react --save-dev
rollup.config.js
at the root level of the project.
react-pack/
├── src/
│ ├── components/
│ │ └── Button.jsx
│ ├── stories/
│ │ └── Button.stories.jsx
│ └── index.js
├── package.json
└── rollup.config.js
import babel from "rollup-plugin-babel";
import resolve from "@rollup/plugin-node-resolve";
import external from "rollup-plugin-peer-deps-external";
import { terser } from "rollup-plugin-terser";
import postcss from "rollup-plugin-postcss";
export default [
{
input: "./src/index.js",
output: [
{
file: "dist/index.js",
format: "cjs",
},
{
file: "dist/index.es.js",
format: "es",
exports: "named",
},
],
plugins: [
postcss({
plugins: [],
minimize: true,
}),
babel({
exclude: "node_modules/**",
presets: ["@babel/preset-react"],
}),
external(),
resolve(),
terser(),
],
},
];
input
- starting pointing of your projectoutput
- where your want to put all the build filesplugins
- plugins to use"scripts": {
"build": "rollup -c"
}
react-pack/
├── dist/
├── src/
│ ├── components/
│ │ └── Button.jsx
│ ├── stories/
│ │ └── Button.stories.jsx
│ └── index.js
├── package.json
└── rollup.config.js
npm login
.package.json
file)package.json
file
- Change main from "index.js" to "dist/index.js"
- Add module field and set it to "dist/index.es.js"
...
"main": "dist/index.js",
"module": "dist/index.es.js",
...
npm publish
Share your package in the comments below. I will try it 👀.
27