---
title: "Discussion on \"Publishing TypeScript React components to NPM\" | Hashnode"
description: "Discussion on \"Publishing TypeScript React components to NPM\". Publishing to NPM can be daunting at first. In this guide we'll make it quick and easy."
url: "https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Publishing TypeScript React components to NPM](https://blog.rupertmckay.com/publishing-typescript-react-components-to-npm?utm_source=hashnode&utm_medium=feed)

Publishing to NPM can be daunting at first. In this guide we'll make it quick and easy.

Jul 17, 2021 · 7 min read · blog.rupertmckay.com  
Updated Sep 30, 2021  
Read the article

-   [#typescript](https://hashnode.com/tag/typescript)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#npm](https://hashnode.com/tag/npm)
-   [#howtos](https://hashnode.com/tag/howtos)
-   [#guide](https://hashnode.com/tag/guide)

## Written by

[Rupert McKay](https://hashnode.com/@fildon)

@fildon · Delft, The Netherlands

  
[View profile](https://hashnode.com/@fildon)

Staff Software Engineer at Lokalise

## Responses 6

Markdown supported · ⌘↵ to post

[Oleksandr Klymenko](https://hashnode.com/@olklymenko)

[Dec 21, 2022](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/63a2d4e129e757b59697c9b1)

Thanks for the article, what should we add if we need to add styling for our component? and our styles should be applied when we import our package.

[Marin](https://hashnode.com/@Marin)

[Jul 7, 2022](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/62c64200c8efe473b8fe5389)

Hi,

Thanks for the tutorial. However, I am facing some issues with Nextjs v12 when importing the component. It is saying that the export token does not exists... I solved it by changing the export to something like:

export \* from './counter';

[Lizhiyu](https://hashnode.com/@lycheefish)

[Mar 17, 2022](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/623291feea00630650dfe10d)

Clearly and comprehensive, great job! Come to be my first bookmark.

[Steve Barakat](https://hashnode.com/@stevebarakat)

[Sep 20, 2021](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/6147de9317da986cea646b63)

Great article. Super easy to follow.

[Steve Barakat](https://hashnode.com/@stevebarakat)

[Sep 20, 2021](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/6147dd45699fe76cd34836bd)

![Screen Shot 2021-09-19 at 8.57.25 PM.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1632099566224/uR9gfTI81.png) Yes. How do you deal with this type of folder structure?

[Rupert McKay](https://hashnode.com/@fildon)

[Sep 22, 2021](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/6147dd45699fe76cd34836bd#reply-614b9c8d544e242e0c93db77)

Hey good question!

I recommend having a single higher level index.ts that gathers together everything you want to export from your library. Guessing from the folder structure you have pictured, you could have an index.ts at: `src/index.ts`

Which contains:

```
export { DualRangeSlider } from './components/DualRangeSlider'
export { DualVerticalRangeSlider } from './components/DualVerticalRangeSlider'
// etc
```

Then your build process just needs to point at that single index file to bundle up everything you want your library to expose.

I'm doing this in the example provided in the article, but it's probably not obvious because I only have a single example component to re-export. But that same pattern works for any number of components, as long as they each have an entry in the top-level index file.

I'll make an edit to the article to make this clearer when I have some time on the weekend 🙂

[Steve Barakat](https://hashnode.com/@stevebarakat)

[Sep 24, 2021](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/6147dd45699fe76cd34836bd#reply-614de96c05ebd95c9caed13e)

[Rupert McKay](https://hashnode.com/@fildon) What's the point in making an `index.ts` file that exports the component from the file it's already being exported from?

`export { Button } from "./Button"`

Is it just for having: `import Button from 'Button'`

instead of: `import Button from 'Button/Button'`

[Rupert McKay](https://hashnode.com/@fildon)

[Oct 18, 2021](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/6147dd45699fe76cd34836bd#reply-616d2c7c441bd36daa957e07)

[Steve Barakat](https://hashnode.com/@stevebarakat)

Yup, that's exactly it!

I want the code I write to be as easy to use as possible for people that depend on it. Maybe the difference between

`from 'Button/Button'`

and

`from 'Button'`

isn't much, but it is something I can do to help even if only a little. The tradeoff is it means I need to have a top-level index file that re-exports everything from a single entry point.

[Aleksandar Mirilović](https://hashnode.com/@amirilovic)

[Jul 17, 2021](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/60f3173266fcdb0a02742804)

Good one Rupert! One question, if package that you are publishing is part of a workspace, would publishing from dist work? Most likely you need to build the package and point main file to dist/index.js in order to use it elsewhere in the workspace, how would you deal with that?

[Rupert McKay](https://hashnode.com/@fildon)

[Oct 18, 2021](https://hashnode.com/posts/publishing-typescript-react-components-to-npm/60f30734bb67011cb8369b7f/comment/60f3173266fcdb0a02742804#reply-616d2dd0f1f4c944cc6b30e9)

Honestly, I've not had much experience with workspaces.

My usual inclination would be to coordinate via npm packages. But I know that's not everyone's preference.

I'd be really interested to read a write-up on how exactly the build process I've described above would need to change in a multi-workspace monorepo.
