How to automate publishing npm packages to the registry

1 min read Original article ↗

Buddy

This article will show you how to configure npm modules in a proper way and use Buddy to automatically test and publish your packages

Objectives of this guide

This guide will show you how to:

  • Create an npm package
  • Publish the package in the registry
  • Automate testing and publishing of the npm package thanks to Buddy

Requirements

The first thing you need to do is to install the following things:

Creating an npm package

Begin with creating a folder and initializing a Git repository so that your package remains safe under version control:

mkdir my_first_npm_module
cd my_first_npm_module
git init

Next, add package.json which will contain the details of your module. You can easily do that by running this command:

npm init

You will be asked to provide the information about your package:

  • name
  • version
  • main value (usually set to index.js)

Once done, check the contents of package.json. It should look like this:

{
"name": "buddy-demo-package",
"version": "1.0.3",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

In index.js the functions should be provided as a property of the exports object. Here's an example:

exports.printMsg = function() {    
console.log("My first package message");
}

Once everything is set, make sure to commit your files to the repo…