How to create a script package for hubot

I recently created a script package (hubot-lmgtfy) for hubot. This post describes what I did to create the package.

Step 1: Install grunt (if not installed already)

npm install -g grunt-cli

Warning: Do NOT use sudo with an npm install. If you get an EACCES error message saying to rerun as an administrator - DO NOT DO IT. Instead follow the instructions on the npmjs.com site to configure your npm permissions. The instructions give two options:

  1. Change the permissions of npm’s default directory
  2. Change npm’s default directory to another directory

I chose to do option two.

Step 2: Generate a hubot script package

Follow the instructions in the hubot documentation to generate a skeleton script package. You should make the following changes to the generated package.json file:

The package.json changes should include something like the following:

"version": "1.0.0",

"repository": {
    "type": "git",
    "url": "git://github.com/YOUR-USERNAME/hubot-your-script.git"
  },

  "bugs": {
    "url": "https://github.com/YOUR-USERNAME/hubot-your-script/issues"
  },

Step 3: Test your npm package locally

UPDATE Dec 2017: The steps below do NOT work for npm 5. Instead, use the npm install or npm link commands as described in this stackoverflow question. For more details see: http://codetunnel.io/npm-5-changes-to-npm-link/

Once you have created your script, you will want to test your package locally. To do so, you will need to make your package available using the npm link command. In the root of your package do the following commands:

npm install
npm link

The npm link command should give you output like the following:

/Users/monica/.npm-global/lib/node_modules/hubot-your-script -> /Users/monica/development/hubot-your-script

This creates a symbolic link to your script package.

Next, cd to your hubot installation and run the following command

npm link hubot-your-script

Which should give you output like the following:

Users/monica/development/myhubot/node_modules/hubot-your-script -> /Users/monica/.npm-global/lib/node_modules/hubot-your-script -> /Users/monica/development/hubot-your-script

This creates a symbolic link from the hubot’s local node_modules directory to the global symlink created in the step above. Note: The script package name you use with this link command should be the name used in your script’s package.json file and not the directory name.

Add your package name to your hubot’s external-scripts.js file

[
  "hubot-your-script"
]

and run hubot

bin/hubot

Trying running your new hubot command. Hopefully it works!

When you are done testing your package locally, unlink it.

In your hubot directory do the following:

npm unlink hubot-your-script

Next, cd to the root of your hubot’s script package and do the following:

npm unlink

Step 4: Publish your package

When you are ready to publish your package, you can do so by following the instructions on npm’s site