Using NPM Scripts for Client-Side Module Installation

One of the largest things that I have loved about working in Node.js is the ability to create utilities that work on both the server and within the browsers.  Being able to create packages for NPM and then reuse them in both environments has lead to a lot of shared and unified code.  However, the general experience involving installing a package and moving it to the client left a lot to be desired.  Thankfully, NPM offers a possible solution through it’s use of scripts.

Using Scripts with NPM

NPM has the ability to run custom scripts at various times of different commands is offers.  For example, defining a test script will be ran when someone runs npm test <package>NPM provides with a number of pre-defined hooks in which scripts that can be used out of the box to automate a number of processes.  In addition to these pre-defined hooks, you can define your own labels in order for the scripts so that they can be ran manually using the run-script command.

For example, if you wish to have a custom script to be executed after someone installs a module for the first time, your package.json file may have the following section defined:

"scripts": {
    "test": "mocha tests/luhn-tests.js -R spec"
    , "install": "node bin/client-install-directions.js"
}

The above snippet of a package.json file, install tells NPM that a script is provided to be ran after someone types in npm install <package>.  This script will be ran automatically which can be used to help configure an environment or, as in this case, provide instructions on how to install the module into a client-exposed JavaScript directory for client-side usage.

Using Custom Scripting Hooks

Using the default events can provide a lot of helpful hooks into NPM for automation and providing additional information to the consumer; however, you can define your own hooks that can be ran with NPM’s run-script command.  Let’s modify the snippet to illustrate this point:

"scripts": {
    "test": "mocha tests/luhn-tests.js -R spec"
    , "install": "node bin/client-install-directions.js"
    , "update": "node bin/client-install-directions.js"
    , "client-install": "node bin/client-install.js"
}

In this modified version, 2 additional lines were provided. First, we added update to do provide the same instructions as install so that a person updating the package will have the same experience as that of a fresh install. Secondly, we added a client-install script which isn’t automatically used by NPM. To run this custom script, we just need to use the following command:

npm run-script <package> client-install

This will tell NPM to look in the scripts section of the package.json file for the pacakge and execute the client-install script.  By defining scripts in this manner, it allows for a number of utility scripts to be implemented without the need of globally install additional shell commands like some modules require.

In the Wild

If you want to see an example of this in action, three packages that I’ve authored have been updated to include these new scripts.  Feel free to review them and provide any feedback on how I can make the experience better.

  • Luhn (1.0.5) – Credit Card (Mod10) Validation (npm, github)
  • ABAValidator (1.0.4) – Routing Number Validation (npm, github)
  • DateValidator (1.0.5) – A Simple Day, Month, Year Date Validator (npm, github)
Advertisements

One thought on “Using NPM Scripts for Client-Side Module Installation

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