In this article, we shall learn how to publish a library to the string-util registry. We shall breakdown the tutorial into the following steps:

  1. Writing the library,
  2. Testing the library
  3. Publishing the library
  4. Bonus: Add cool badges, we are cool, ain’t we?
  5. Further areas to look at.

1. Writing the library

A library is a reusable chunk of functionality that one can drop into their project and make use of functionalities without having to come up with their own set of functionalities. Kinda hot right?

  1. Create a node project:
mkdir string-util
cd string-util
npm init -y

yarn init -y or npm init -y creates an empty Nodejs project with a package.json file with some default project metadata defaults.

We shall create a dummy library that converts strings to uppercase and lowercase to serve the purpose of this tutorial:

// A dummy function to convert string to uppercase.
function toUpperCase(str) {
  if (typeof str !== 'string') throw new Error('Argumnet supplied must be of typeof string')
  else return str.toUpperCase()

function toLowerCase(str) {
  if (typeof str !== 'string') throw new Error('Argumnet supplied must be of typeof string')
  else return str.toUpperCase()

// Expose function to be used as a module.
// module.exports: 
// Read more:
// For ES6 Modules, AMD Modules, Commonjs:
module.exports = { toUpperCase, toLowerCase }

  1. Add a, this will show on the package main page when viewed on npm, Let’s add some documentation for our tiny library:
## string-utils
`string-utils` is a tiny library that provides functionality to convert strings to `uppercase` and `lowercase`

### Example usage:

The structure of an npm library goes as follows:

  1. A top-level index.js file with all functionality exported. Without this, the library won’t work.
  2. Logic on separate files exported as modules, index.js acts as an interface to expose all these functionalities for use elsewhere.

2. Publishing our library

To publish our library, we need:

  1. To test our library locally
  2. A version number to identify our release
  3. Publishing to NPM To be logged in to npm from our terminal

1. Testing locally

To test the installation locally, we:

cd string-util/lib/
yarn test-install

To test the usage:

// index.js
const { toUppercase } = require('string-utils')

Please note that these are dummy methods that serve our example since they all exist on String. prototype

2. Setting version number

NPM suggests that all initial releases should be set to v1.0.0, the initial version property on package.json is usually set to the major version. All versions are set following the semver versioning guidelines.

3. Publishing the library to NPM 🚀

To publish the module:

  1. cd into your lib folder: cd lib/
  2. Run npm login - Requires, an npm account
  3. After successful login, run npm publish
  4. Library will be uploaded and published to npm, at version 1.0.0

Things to note:

  1. Your lib, (a name chosen for convenience), is important to have:
  • A package.json file, (at the root)
  • A brief file describing how to install and use the library(this shows on the package details page on npm)
  • A top-level index.js file to act as an interface that exposes all library functionality