<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Chan Jing Hong</title>
    <description>The latest articles on Forem by Chan Jing Hong (@cjinghong).</description>
    <link>https://forem.com/cjinghong</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366553%2F77397aff-ea28-4831-9134-13656e03ecb5.jpeg</url>
      <title>Forem: Chan Jing Hong</title>
      <link>https://forem.com/cjinghong</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cjinghong"/>
    <language>en</language>
    <item>
      <title>How to publish your React component on npm</title>
      <dc:creator>Chan Jing Hong</dc:creator>
      <pubDate>Tue, 14 Apr 2020 14:04:28 +0000</pubDate>
      <link>https://forem.com/cjinghong/publishing-your-first-react-component-to-npm-7bb</link>
      <guid>https://forem.com/cjinghong/publishing-your-first-react-component-to-npm-7bb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This tutorial is for the more experienced React developers. If you’re just looking to learn how to build a &lt;a href="https://reactjs.org/"&gt;React app&lt;/a&gt;, there are tons and tons of tutorials available online!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, so you’ve read tutorials, figured out how to set up a React project using &lt;code&gt;create-react-app&lt;/code&gt;, learned how to install npm packages, and finally created your very own functional React app, and to that that I say congratulations!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NIG4WElJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2Ao6QbQCwuPjbSTS6NJhhtSg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NIG4WElJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2Ao6QbQCwuPjbSTS6NJhhtSg.png" alt="alt text" title="Congratulations"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you go on your journey of creating your apps, you will undoubtedly come across some cool open source npm packages that made your development easier, whether it be &lt;a href="https://www.npmjs.com/package/@material-ui/core"&gt;material-ui&lt;/a&gt;, &lt;a href="https://www.npmjs.com/package/react-router-dom"&gt;react-router&lt;/a&gt;, or &lt;a href="https://www.npmjs.com/package/react-spinners"&gt;react-spinners&lt;/a&gt;. Heck, chances are you might’ve even come up with some of your own cool React components! And now you’re wondering, how might I share that with the rest of the world?&lt;/p&gt;




&lt;h1&gt;
  
  
  Packaging your React component
&lt;/h1&gt;

&lt;p&gt;Before publishing anything to npm, the first step is to put your component files into a nice, clean package. I’ve created a template to make everything easier to follow, go ahead and &lt;a href="https://github.com/Groftware/template-reactnpm"&gt;download this&lt;/a&gt; first. And once you’re done, open the folder in your favorite code editor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g6dvn9ok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/900/1%2AQj35PGdY8C59bxibYig6Yg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g6dvn9ok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/900/1%2AQj35PGdY8C59bxibYig6Yg.png" alt="alt text" title="Visual Studio Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;my favorite one is definitely &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Adding your React component
&lt;/h2&gt;

&lt;p&gt;The first thing you’re gonna wanna do is to move your React component files into the &lt;code&gt;/src&lt;/code&gt; folder. For the purpose of this demonstration, I’ve created a component called &lt;code&gt;MyCoolButton.js&lt;/code&gt; . Make sure to also include all the files that are required by your component. In this case, I‘ve also added in &lt;code&gt;MyCoolButton.css&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7yVsQ-Vk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1128/1%2A6f3MFY2r2ZYHb5VkcP33pA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7yVsQ-Vk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1128/1%2A6f3MFY2r2ZYHb5VkcP33pA.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add your component files into /src folder&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Sort out dependencies
&lt;/h2&gt;

&lt;p&gt;The next thing you have to do is to figure out if your component requires any other dependencies. You can find that out by opening up your component file and check if it imports any other dependencies. In this case, my component requires the dependency &lt;code&gt;prop-types&lt;/code&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This component also requires 'react',
// but it has already been included in the
// list of dependencies in package.json
import React from 'react';

// This component requires prop-types
import PropTypes from 'prop-types';

import './MyCoolButton.css';

const MyCoolButton = ({ type, title, onClick }) =&amp;gt; (
  &amp;lt;button
    type={type}
    className="container"
    onClick={onClick}
  &amp;gt;
    {title}
  &amp;lt;/button&amp;gt;
);

MyCoolButton.propTypes = {
  title: PropTypes.string.isRequired,
  type: PropTypes.string,
  onClick: PropTypes.func,
};

MyCoolButton.defaultProps = {
  type: 'button',
  onClick: () =&amp;gt; {},
};

export default MyCoolButton;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let’s open up &lt;code&gt;package.json&lt;/code&gt; , and add the dependencies in. Normally you would add your dependencies under &lt;code&gt;dependencies&lt;/code&gt; , but in this case, you have to add them into &lt;code&gt;peerDependencies&lt;/code&gt; and &lt;code&gt;devDependencies&lt;/code&gt; . This is how your &lt;code&gt;package.json&lt;/code&gt; should look like. Notice that the list of dependencies is empty&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "name": "YOUR_PACKAGE_NAME",
    "version": "0.0.1",
    "description": "",
    "main": "./lib/YOUR_COMPONENT.js",
    "license": "MIT",
    "scripts": {
        "build": "webpack"
    },
    "peerDependencies": {
        "prop-types": "^15.6.0",
        "react": "^16.0.0",
        "react-dom": "^16.0.0"
    },
    "devDependencies": {
      "prop-types": "^15.6.0",
        "babel-core": "^6.21.0",
        "babel-loader": "^7.1.4",
        "babel-preset-env": "^1.6.1",
        "babel-preset-react": "^6.16.0",
        "babel-preset-stage-0": "^6.24.1",
        "css-loader": "^3.5.1",
        "path": "^0.12.7",
        "react": "^16.0.0",
        "react-dom": "^16.0.0",
        "style-loader": "^1.1.3",
        "webpack": "^4.5.0",
        "webpack-cli": "^3.2.1"
    },
    "dependencies": {}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After that, run &lt;code&gt;npm install&lt;/code&gt; (or if you use &lt;a href="https://yarnpkg.com/"&gt;yarn&lt;/a&gt;, &lt;code&gt;yarn install&lt;/code&gt;) to install the required dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. webpack.config.js
&lt;/h2&gt;

&lt;p&gt;Next up, we’ll use Webpack to bundle our React components into a nice CommonJS module. &lt;a href="https://webpack.js.org/"&gt;Click here&lt;/a&gt; to learn more about Webpack and what it does. Let’s start by opening up &lt;code&gt;webpack.config.js&lt;/code&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');

module.exports = {
  mode: 'production',
  entry: './src/MyCoolButton.js',
  output: {
    path: path.resolve('lib'),
    filename: 'MyCoolButton.js',
    libraryTarget: 'commonjs2',
  },
  module: {
    rules: [
      {
        test: /\.js?$/,
        exclude: /(node_modules)/,
        use: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      }
    ],
  },
  resolve: {
    alias: {
      'react': path.resolve(__dirname, './node_modules/react'),
      'react-dom': path.resolve(__dirname, './node_modules/react-dom'),
    }
  },
  externals: {
    // Don't bundle react or react-dom      
    react: {
      commonjs: "react",
      commonjs2: "react",
      amd: "React",
      root: "React"
    },
    "react-dom": {
      commonjs: "react-dom",
      commonjs2: "react-dom",
      amd: "ReactDOM",
      root: "ReactDOM"
    }
  }
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is the file that contains the different configurations that Webpack will use to bundle your React component. Here’s a description of the important settings:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;entry&lt;/code&gt; — This refers to your React component. You should change it to the component that you’ve added in &lt;strong&gt;Step 1&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;output&lt;/code&gt; — This specifies the path to the output file. You should update the &lt;code&gt;filename&lt;/code&gt; to match your component’s file name.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;module.rules&lt;/code&gt; — This is an array of rules that we are applying to our module. The first rule looks for any &lt;code&gt;.js&lt;/code&gt; file and tries to &lt;a href="https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them"&gt;transpile&lt;/a&gt; it using &lt;code&gt;babel-loader&lt;/code&gt; . However, the second rule is only relevant if your component uses &lt;code&gt;css&lt;/code&gt; . If your component uses any &lt;code&gt;css&lt;/code&gt;, you will have to add this in. Click here to find out more about &lt;code&gt;[css-loader](https://webpack.js.org/loaders/css-loader/)&lt;/code&gt; and &lt;code&gt;[style-loader](https://webpack.js.org/loaders/style-loader/)&lt;/code&gt; .&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important Note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your component uses &lt;code&gt;.sass&lt;/code&gt; or&lt;code&gt;.scss&lt;/code&gt; , you would have to also include &lt;code&gt;[sass-loader](https://webpack.js.org/loaders/sass-loader)&lt;/code&gt; by adding it to your Webpack module rules as well as adding it to your list of &lt;code&gt;devDependencies&lt;/code&gt; in &lt;code&gt;package.json&lt;/code&gt; . &lt;a href="https://webpack.js.org/loaders/"&gt;Click here&lt;/a&gt; to see a list of loaders that you might need to use.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I have only gone through the options that are relevant to this tutorial. Check out the full list of options &lt;a href="https://webpack.js.org/configuration/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Bundle em’ up
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;npm run build&lt;/code&gt; (or if you use yarn, &lt;code&gt;yarn build&lt;/code&gt;). This should generate a folder called &lt;code&gt;/lib&lt;/code&gt; which contains your freshly packaged component, in this case, &lt;code&gt;MyCoolButton.js&lt;/code&gt; .&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Test your component
&lt;/h2&gt;

&lt;p&gt;Before you publish it to the world, it would make sense to take your package for a test drive first (or if you’re absolutely confident that it will work, feel free to skip ahead to &lt;strong&gt;Publishing To NPM&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;npm pack&lt;/code&gt; . This will generate a &lt;code&gt;.tgz&lt;/code&gt; file at the root directory.&lt;/p&gt;

&lt;p&gt;Open up any React application that you want to test your new package, and then run &lt;code&gt;npm install path_to_tgz_file&lt;/code&gt; . Replace &lt;code&gt;path_to_tgz_file&lt;/code&gt; with your actual path.&lt;/p&gt;

&lt;p&gt;In my case, the whole command looks like this: &lt;code&gt;npm install /Users/Groftware/mycoolbutton/mycoolbutton-0.0.1.tgz&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;The next thing you do is just import the component and use it normally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';  
import MyCoolButton from 'mycoolbutton';function App() {  
 return (  
    &amp;lt;div&amp;gt;  
      &amp;lt;MyCoolButton title="Click Me!"/&amp;gt;  
    &amp;lt;div&amp;gt;  
  )  
}export default App;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Does it work? Great! Let’s move on to actually publishing it to the world.&lt;/p&gt;

&lt;h1&gt;
  
  
  Publishing to NPM
&lt;/h1&gt;

&lt;p&gt;Alright so now you have your &lt;code&gt;/lib&lt;/code&gt; folder with your newly packaged component ready to go, the next thing to do it just publishing it to npm.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;npm login&lt;/code&gt; and login with your npm account. Create one &lt;a href="https://www.npmjs.com/signup"&gt;here&lt;/a&gt; if you don’t already have one.&lt;/p&gt;

&lt;p&gt;After you’re logged in, the final thing to do is &lt;code&gt;npm publish&lt;/code&gt; , and you’re all set!&lt;/p&gt;




&lt;h1&gt;
  
  
  Links
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Tutorial Template and complete example
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://github.com/Groftware/template-reactnpm"&gt;Template&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://github.com/Groftware/MyCoolButton"&gt;Full example&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.npmjs.com/package/mycoolbutton"&gt;Published npm package&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  React
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://reactjs.org/"&gt;https://reactjs.org/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Npm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://www.npmjs.com/signup"&gt;https://www.npmjs.com/signup&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.npmjs.com/package/@material-ui/core"&gt;https://www.npmjs.com/package/@material-ui/core&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.npmjs.com/package/react-router-dom"&gt;https://www.npmjs.com/package/react-router-dom&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://www.npmjs.com/package/react-spinners"&gt;https://www.npmjs.com/package/react-spinners&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Yarn
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://yarnpkg.com/"&gt;https://yarnpkg.com/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Webpack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://webpack.js.org/"&gt;https://webpack.js.org/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://webpack.js.org/loaders/css-loader/"&gt;https://webpack.js.org/loaders/css-loader/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://webpack.js.org/loaders/style-loader/"&gt;https://webpack.js.org/loaders/style-loader/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://webpack.js.org/loaders/sass-loader"&gt;https://webpack.js.org/loaders/sass-loader&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://webpack.js.org/loaders/"&gt;https://webpack.js.org/loaders/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Enjoyed or found this helpful? Checkout my other articles &lt;a href="https://medium.com/groftware"&gt;here&lt;/a&gt;. Thanks!&lt;/p&gt;

</description>
      <category>react</category>
      <category>npm</category>
      <category>component</category>
      <category>package</category>
    </item>
  </channel>
</rss>
