Posted on: Written by: K-Sato
⚠️ This article was posted over 2 years ago. The information might be outdated. ⚠️

Table of Contents

Creating your package

Simply Follow the code below.

# Create the project directory
$ mkdir samplePackage

# Change into the project directory
$ cd samplePackage

# Initialise an NPM package(It would create th package.json for you)
$ npm init

# Create the entry point
$ touch index.js

Write some code

Whatever you put in module.exports is what would be available for importing when others install the package.

const Test = 'Test'
function Sum(a, b) {
  return a + b
}

module.exports = { Test, Sum }

Publish it

Simply, run

$ npm publish

Extra stuff

Get badges at Shields.io: Quality metadata badges for open source projects

Test your package locally

npm install /absolute/path/yourPackage

Or you can create index.html like below and test it out there.

<!DOCTYPE html>
<html>
  <head>
    <script src="index.js"></script>
  </head>
  <body>
    <h1>Test HTML FILE</h1>
  </body>
</html>

Creating react components as a package

Use create-react-library.

Make sure to add a .gitignore file like below

.gitignore
/node_modules
/example/node_modules

Use the latest version of your package locally

Make sure to run the command below to use the the latest version of your package locally.

$ yarn build

Passing props

You can pass props like the code below.

  static propTypes = {
    markdownText: PropTypes.string,
    test: PropTypes.boolean
  }

// To access the defined porps.
this.props.markdownText

References

About the author

I am a web-developer based somewhere on earth. I primarily code in TypeScript, Go and Ruby at work. React, RoR and Gin are my go-to Frameworks.