Sunday, August 26, 2012

Create a npm package

npm is package manager for nodejs.

If you want to create and publish npm package you must complete theses steps:

1. Create a profile

Go to npm website and create a profile.

2. Create package.json file

Create a folder for your package and change the current directory to it:
mkdir my_package
cd my_package
Create package.json file:
{ 
  "name" : "my_package",
  "description": "package description goes here",
  "version": "1.0.0",
  "author": "Firstname Lastname <firstname.lastname@mail.com>",
  "keywords": ["my_keyword1", "my_keyword2"], 
  "repository": {
    "type": "git",
    "url": "https://github.com/myuser/my_package.git"
  },
  "main" : "./lib/my_package.js",
  "engines": {
    "node": ">=0.8.8"
  }
}
3. Link with npm

Link your package with npm:
npm link
4. Publish the package

Add a user:
npm adduser
Publish a package:
npm publish
If you want to override published package you can user --force parameter:
npm publish --force

Monday, August 13, 2012

Say 'Hello world' using Node.js

Install node.js on Ubuntu

Install the dependencies:
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core
Download source code of version 0.8.8 from github:
git clone -b v0.8.8-release git://github.com/joyent/node.git
Build and install node.js:
cd node
sudo ./configure
sudo make
sudo make install
To get node.js version type:
node --version
Create Hello world application

Create my-script.js file:
console.log("Hello world!!!")
Run the code:
node my-script.js
Output:
Hello world!!!