DEV Community

Cover image for Publish Your First Node Library Using NPM
Juan Cruz Martinez
Juan Cruz Martinez

Posted on • Originally published at livecodestream.dev on

Publish Your First Node Library Using NPM

If you are a Node.js developer, using npm packages won’t be a new concept to you. From complex npm packages like express to simple packages, npm hosts a large number of Node.js libraries that simplify the workload of Node developers. But have you ever thought about publishing a Node library of your own to help fellow developers? In today’s tutorial, we are going through the steps you have to follow when publishing your own Node.js library using npm.

Before we begin…

Before we begin this, you should have the following things set up.

If you have all of the above set up, let’s get to publishing the library.

In this tutorial, we are creating a simple library that converts a sentence to title case (in which the first letter of every word is capitalized).


Choose a package name

When choosing a name for the npm package, you have to check whether the name is available on the official npm website. Your package name should be unique if you are publishing it as an unscoped (public) package.

However, if the package is scoped, or private, the package name does not have to be unique and the name takes the format of @username/package-name. You can find out more about scoped packages here.

The name of the package we are going to create is “title-case-converter”.


Create a GitHub repository

Create a new repository named “title-case-converter” in GitHub. Make sure to initialize the repository with a README. Choose an appropriate license like the MIT license as well.


Set up the author and log in to npm

Save your name, email, and website (if any) to npm. These details will be added to the package.json file of the project when it’s created.

npm set init.author.name "<your-name>"
npm set init.author.email "<your-email-address">
npm set init.author.url "<your-website-url>"

Enter fullscreen mode Exit fullscreen mode

Then, log in to npm with the command npm login. You will be prompted to enter the credentials you used to create the npm account.


Initialize the project

Create a new directory for the project and initialize it using the npm init command. Make sure to fill out package-name , version , description , git repository , keywords , and license fields to add these details to the package.json file.

(base) ➜ title-case-converter npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (title-case-converter)
version: (1.0.0)
description: Capitalizes the first letter of every word in a sentence
entry point: (index.js) app.js
test command: mocha
git repository: https://github.com/<username>/title-case-converter
keywords: title-case, converter
author: bajcmartinez
license: (ISC) MIT
About to write to /Users/zojcruzm/Projects/title-case-converter/package.json:

{
  "name": "title-case-converter",
  "version": "1.0.0",
  "description": "Capitalizes the first letter of every word in a sentence",
  "main": "app.js",
  "scripts": {
    "test": "mocha"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/%3Cusername%3E/title-case-converter.git"
  },
  "keywords": [
    "title-case",
    "converter"
  ],
  "author": "bajcmartinez",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/%3Cusername%3E/title-case-converter/issues"
  },
  "homepage": "https://github.com/%3Cusername%3E/title-case-converter#readme"
}

Is this OK? (yes) yes
(base) ➜ title-case-converter

Enter fullscreen mode Exit fullscreen mode

(the author fill may not be asked if you are already logged in and with the author set up in the initial documentation as we did above).

The created package.json file contains these details.

{
  "name": "title-case-converter",
  "version": "1.0.0",
  "description": "Capitalizes the first letter of every word in a sentence",
  "main": "app.js",
  "scripts": {
    "test": "mocha"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/%3Cusername%3E/title-case-converter.git"
  },
  "keywords": [
    "title-case",
    "converter"
  ],
  "author": "bajcmartinez",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/%3Cusername%3E/title-case-converter/issues"
  },
  "homepage": "https://github.com/%3Cusername%3E/title-case-converter#readme"
}

Enter fullscreen mode Exit fullscreen mode

Additionally, you can add another field named engines to the package.json file to specify the lowest version of Node.js that your project is compatible with.

"engines": {
    "node": ">= 8.0.0"
 },

Enter fullscreen mode Exit fullscreen mode

A little about the version

Note how we are using the version 1.0.0 for this package. This is the version we should use for the initial version of a package published to npm.

Npm uses a versioning system called Semantic Versioning (SemVer). The default starting version is 1.0.0. In future updates of your package, there are three types of version changes you can make following SemVer standards. These 3 are named patch, minor, and major.

  1. MAJOR version when you make incompatible API changes,
  2. MINOR version when you add functionality in a backwards compatible manner, and
  3. PATCH version when you make backwards compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

You can read more about SemVer versioning here.


Create the Node module

Now we can implement our package, title-case-converter.

Create a file named app.js (or whichever you set as your entry point) and add the following code to it.

function converter(sentence){
    let capitalized = []
    let words = sentence.split(" ") //split the sentence into words
    words.forEach(word => { 
        let capitalizedWord = word.slice(0, 1).toUpperCase() + word.slice(1) //capitalize the first letter of every word
        capitalized.push(capitalizedWord)         
    })
    let converted = capitalized.join(" ") 
    return converted
}

module.exports = converter

Enter fullscreen mode Exit fullscreen mode

It contains a single function called converter, which accepts a string sentence and capitalizes the first letter of every word in it. The most important part of the above code is exporting the created function using module.exports.


Initialize Git

Now, we are going to initialize git for our project using the command git init. Then, stage the files we changed, using the following command.

git add .

Enter fullscreen mode Exit fullscreen mode

In the next step, commit the changes of code to the local repository.

git commit -m "initial changes"

Enter fullscreen mode Exit fullscreen mode

After this, we have to add the GitHub repository as the project’s remote repository.

git remote add origin <GitHub repository URL>

Enter fullscreen mode Exit fullscreen mode

Pull the changes from the remote repository and then push the local changes to it.

git pull origin master
git push origin master

Enter fullscreen mode Exit fullscreen mode

Write a good README file for the package

Including a good README is crucial when publishing an npm package. It gives the other users the basic rundown of what your package does and how they can use it in their projects.

You can find a template to create a good README here. To get a better idea about how your README should look, you can browse popular npm packages and GitHub repositories to see how others have done it.

Generally, a README should cover the following fields.

  • A small description of what your package does.
  • How another user can install the package to use it in their project.
  • How to use the package in a project. Provide code examples explaining the usage.
  • How to contribute to your package. If you expect other open-source developers to contribute to this project, this field is a must-add to the README file.
  • The license used by the package. For this package, we used the MIT license.

After these changes don’t forget to commit and push.


Publish the package to npm

Even though the concept of publishing a library feels like a lot of work when you first hear about it, this step is surprisingly simple. All you have to do is run the following command in your command-line.

npm publish

Enter fullscreen mode Exit fullscreen mode

If you chose a package name that doesn’t clash with existing npm packages, this step should go smoothly and in a matter of seconds, your first package will be published to npm.

Now you can create a new Node.js project and install your new package as a dependency using this command.

npm install title-case-converter --save

Enter fullscreen mode Exit fullscreen mode

Then, you can import the package to the new project and use it to convert sentences to the title-case as you wish.

const converter = require("title-case-converter")

let convertedSentence = converter("my name is juan") //"My Name Is Juan"

Enter fullscreen mode Exit fullscreen mode

That’s it! You are now a proud owner of a published npm package.

You can try your hand at creating a more complex Node.js module and publish it to npm after this.


Conclusion

As you may of have expected publishing an NPM package is a simple task, and you don’t always need to follow the same steps in the same order like it was explained in this tutorial. You can assign the author, or make modifications to the package name or any other attributes later on by using npm commands or simply editing the package.json file.

I hope you learned something new today, and as always, thanks for reading!


If you like the story, please don't forget to subscribe to our free newsletter so we can stay connected: https://livecodestream.dev/subscribe

Top comments (0)