Create a globally accessible node.js module

I would like to create a globally accessible node.js module, like express and mocha.

So I dived into their source code, and created the following short example:

The index.js file, located in the hello/ directory:

#!/usr/bin/env node

console.log("Hello world!")

As you can see, it's just a "hello world" to test the concept.

And here is the package.json file located in the same hello/ directory:

{
  "name": "Hello",
  "version": "0.0.1",
  "main": "./index",
  "bin": {
    "hello": "./bin/hello"
  }
}

And then, I tried to install the node app with npm with the following:

sudo npm install -g hello/

But this return an error:

npm ERR! Error: ENOENT, chmod '/usr/local/lib/node_modules/Hello/bin/hello'

I don't know what is wrong, since I respected the module structure like I found it on express and mocha. Can you provide me a help by:

  • pointing what's wrong in my case
  • linking to useful tuts and resources about creating executable node modules

Thanks.

You are attempting to install the directory.

If you want to install the module locally for you, just copy it to /usr/local/lib/node_modules/npm/node_modules/.

However, if you want npm to install it (to share), you will have to create a package.

npm login
npm publish
npm install -g hello

If you are shy, npm unpublish will remove it.