Introduction
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:
- Writing the library,
- Testing the library
- Publishing the library
- Bonus: Add cool badges, we are cool, ain’t we?
- 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?
- Create a
node
project:
mkdir string-util
cd string-util
npm init -y
yarn init -y
ornpm init -y
creates an emptyNodejs
project with apackage.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: https://nodejs.org/api/modules.html
// For ES6 Modules, AMD Modules, Commonjs: https://medium.com/computed-comparisons/commonjs-vs-amd-vs-requirejs-vs-es6-modules-2e814b114a0b
module.exports = { toUpperCase, toLowerCase }
- Add a
README.md
, 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:
- A top-level
index.js
file with all functionality exported. Without this, the library won’t work. - 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:
- To test our library locally
- A version number to identify our release
- 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:
- cd into your lib folder:
cd lib/
- Run
npm login
- Requires, an npm account - After successful login, run
npm publish
- Library will be uploaded and published to npm, at version
1.0.0
Things to note:
- Your
lib
, (a name chosen for convenience), is important to have:
- A
package.json
file, (at the root) - A brief
README.md
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