How to Build an NPM Package, Beginning the Symphonize Project

NPM has helped to build on the massive Node.js popularity and drive JavaScript from a simple scripting language in the web browser to a powerful and capable back-end server language. A quick refresher, NPM stands for Node.js Package Manager and each package is made up of:

  1. a folder containing a program described by a package.json file.
  2. a gzipped tarball containing [1]
    1. a url that resolves to [2]
    2. a <name>@<version> that is published on the registry with [A]
    3. a <name>@<tag> that points to [B]
    4. a <name> that has a “latest” tag satisfying [C]
    5. a git url that, when cloned, results in [1]
Path structure view in Jetbrains Webstorm IDE.

Path structure view in Jetbrains Webstorm IDE.

With that basic understanding of what a module is that NPM provides, let’s jump through the steps to build a module that provides some basic functionality. I won’t cover too many parts in detail yet, just the happy path to getting an NPM library running.

First let’s create an appropriate folder and file structure to get started with. Here’s the commands I ran to get started.

mkdir bin
mkdir lib

With these two directories created I then created the following files in the designated paths. In bin I created the symphonize.js file and in lib I created a main.js file.

Now, I added the following code to the symphonize.js file.

exports.Coupling = function (searchThis, forThis) {
    var returnValue = 'no';
    if (searchThis.indexOf(forThis) > -1) {
        returnValue = 'yes';
    }
    return returnValue;
}

In the main.js file I added the following.

(function () {
    var couple = require('../bin/symphonize');
    couple.Coupling("Sample text", "Sample");
}).call(this)

There are a number of issues with this code, I know, but it’s just a sample of the minimal amount of code, folder structure and packages.json that I need to get this package installed and ready for iteration as I move forward with the actual code base and what functionality will actually be added. Speaking of the packages.json file, I created one and added the following configuration settings to it.

{
    "author": "Adron Hall",
    "name": "symphonize",
    "description": "Prints out data to the console! Will be iterating soon for real functionality!",
    "version": "0.1.0",
    "repository": {
        "url": "git@github.com:Adron/symphonize.git"
    },
    "main": "./lib/main",
    "bin": {
        "replaceme": "./bin/symphonize"
    },
    "dependencies": {},
    "devDependencies": {},
    "optionalDependencies": {},
    "engines": {
        "node": "*"
    }
}

That is now enough for me to at least get the module added to the global NPM repository, get things pointed back to Github appropriately and move forward with actual coding. I might even setup some continuous builds and delivery at some point, since I’ve now got the end point of where the libraries will be going. The commands to get a module uploaded to the NPM Repository are as follows. This command of course assumes I’ve already added a user using npm adduser or I’ve added one via the web site interface at https://npmjs.org/.

npm publish

I’ve now got everything prepared and uploaded to NPM there is now a symphonize module library ready for use.

My NPM Page for Symphonize. Click to go to the actual NPM page.

My NPM Page for Symphonize. Click to go to the actual NPM page.

Here’s a few quick references to where everything is:

5 thoughts on “How to Build an NPM Package, Beginning the Symphonize Project

  1. Orchestrate.io, Webstorm, Hacking Some Code and Project Pivots | Composite Code

  2. History of Symphonize.js – JavaScript Client Pivot to Data Generation Library | Composite Code

  3. History of Symphonize.js – JavaScript Client Pivot to Data Generation Library – Composite Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s