Hemanth's Scribes

nodejs

Hitchhiker's guide to npm

Author Photo

Hemanth HM

Hello hackers, this post is just a simple hello-world packing tutorial for packing nodejs with npm.

P.S: This does not talk about how npm works in depth, but gets one started with npm and assumption is you have dealt with some nodejs and npm installs before!

First up, if you have not installed node.js and npm (which is bundled with latest version) please do!

The below are the steps for packing and publishing packages to npm registry:

# mkdir hello-node
# cd hello-node/
# npm adduser hemanth
Username: hemanth
Password: 
Email: hemanth.hm@gmail.com
npm http PUT https://registry.npmjs.org/-/user/org.couchdb.user:hemanth
npm http 200 https://registry.npmjs.org/-/user/org.couchdb.user:hemanth
# npm init
Package name: (hello) 
Description: Hello World example for nodejs
Package version: (0.0.0) 
Project homepage: (none) https://www.github.com/hemanth/hello-node
Project git repository: (none) https://www.github.com/hemanth/hello-node.git
Author name: Hemanth.HM
Author email: (none) hemanth.hm@gmail.com
Author url: (none) h3manth.com
Main module/entry point: (none) hello.js
Test command: (none) 
About to write to /home/hemanth/lab/hello-node/package.json
{
  "author": "Hemanth.HM <hemanth.hm@gmail.com> (h3manth.com)",
  "name": "hello",
  "description": "Hello World example for nodejs",
  "version": "0.0.0",
  "homepage": "https://www.github.com/hemanth/hello-node",
  "repository": {
    "url": "https://www.github.com/hemanth/hello-node.git"
  },
  "main": "hello.js",
  "dependencies": {},
  "devDependencies": {},
  "optionalDependencies": {},
  "engines": {
    "node": "*"
  }
}
# ls
package.json

# cat > hello.js
var world = function() { return "Hello World"; }  
exports.world = world
> require('./hello.js')
{ world: [Function] }

> hello = require('./hello.js')
{ world: [Function] }

> hello.world
[Function]

> hello.world()
'Hello World'
# npm publish

That’s it! npm unpublish to remove your package from the registry.

Summarizing the steps:

  • mkdir <package-name>
  • cd <package-name>
  • npm adduser <username>
  • npm init
  • npm publish

A bit of digging into npm — node package manager:

npm is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently.

  • npm-init → Interactively create a package.json file. Make sure that you provide the Main module/entry point.
  • npm-adduser → Adds a registry user account.
  • npm-publish → Publish a package to the registry so that it can be installed by name.
  • npm-unpublish → To remove the package from the registry.

Adding dependencies:

After npm init a package.json file will be created in your package directory, open it and look for the dependencies attribute and add the needed dependencies like below:

"dependencies": {
    "request": ">= 1.2.0",
    "xkcd-imgs": ">=1.0.0"
}

P.S: The most common mistake would be forgetting the exports statement ;)

I think this is enough for starting up, so far was a Hitchhiker’s guide to npm, do let me know if something more can be added for one to just get started with npm!

#javascript#nodejs