Node allows us to easily build fast and scalable networked applications. Every technology emerges out of a reason and node is the answer to networked apps like chat applications which have high concurrent usage scenarios.
Why all the buzz?
Node has been widely adopted due to the following reasons:
- program in javascript - widely popular technology plus a strong engine in the form of V8 to back it up
- simplicity - core functionality is kept at a minimum. In case you need more functionality, several third party plugins are made available. Node Package Manager(NPM) also manages the dependency issues for you. Its hell easy!
- Installs easily on Windows, Linux, Mac and Solaris
Installation
Follow the installation procedures at http://nodejs.org to install the latest stable version on your PC. The unstable versions are battlegrounds for the Node Core development team to test out various new features. Unless you want any of the new features, it is recommended to use stable versions for production. Even minor version packages represent stable builds.
Why Node Package Manager?
Using Node alone would be tedious in building huge applications as the language features and the core low-level APIs dont account for a lot of flexibility.
Using Node alone would be tedious in building huge applications as the language features and the core low-level APIs dont account for a lot of flexibility.
By low-level APIs we mean for e.g. say manipulating data packets in the TCP data packets, opening and closing sockets etc
Thus for Node, there will need to be built several modules before you can actually start on a project. Waste of time!
Luckily today, there are package managers for technology platforms that keep track of a public repository of third party modules and plugins, download and manage them on the local machine during the development phase and also manage the dependency issues for various versions. For Node, this is done by the Node Package Manager. NPM provides command line tools to download, install and manage these plugins.
The central repository of 3rd party plugins may be found at http://search.npmjs.org
Since Node 0.6.0 the NPM is included with the Node installer so no separate installs!
More about NPM
NPM installs the 3rd party modules in 2 locations depending on the installation command specified - locally or globally.
Locally - is within the app directory. For e.g. /home/user/apps/dimple_supi/node_modules
Local installation is the default installation place for Node when you type say
$ npm install dimple
Globally - is within the default install location of Node. For e.g. /usr/local/lib/node_modules . Modules installed here would be available for all the projects and are usually used for command line utilities like express etc. For installing a module globally, precede the module name with -g:
$ npm install -g dimple
When an expression such as var supi = require('dimple'); is encountered in the app js file, Node will first prefer to look up the module locally before proceeding to look it up in the global space. Thus modules installed locally would precede over the ones installed globally.
A few useful NPM commands
$ npm install dimple@">=0.1.0 <0.3.1" - installs a version of dimple that is greater than 0.1.0, nearer to 0.3.1 but not equal to or greater than it. So between dimple@0.2.2 and dimple@0.2.3, the npm would choose dimple@0.2.3 as its nearer to 0.3.1
$ npm install dimple@0.2.x - installs the latest release in 0.2 branch
$ npm uninstall dimple - uninstalls dimple from the local directory. Can be used to remove global installations as well.
$ npm update -g dimple - updates the module to the most latest release. Can be used locally as well.
$ npm update dimple@0.2.x - you should know by now ;)
Using the Executables
Some modules contain one or more executable files. For global installation of such modules, the PATH variable is set to contain this executable and the default installation directory of the executable is /usr/local/bin.
In the case of local installation of such modules, the executables can be located within the node_modules directory with the app directory. For e.g. ./node_modules/.bin
Resolving dependencies
NPM automatically fetches and installs any modules that the current module depends upon. For e.g.
$ npm install nano
output:
nano@0.9.3 ./node_modules/nano
---- supi@1.1.7
---- dimple@2.1.1
Dependencies are managed by package.json, a JSON file in the root directory of the app.
The Node modules were initially conceived to be public packages and not private applications. Therefore the name and description attributes in the package.json file were mandatory and they still are. However you can now choose not to make the modules public.
The package.json usually contain the following - name, version, description, authors, repository, contacts, dependencies. Here is an example:
{
"name":"supi_dimple",
"version":"1.0.0",
"dependencies":{
"sex":"1.1.x",
"dimple":"*",
"supi":">2.3.1"
}
}
"name":"supi_dimple",
"version":"1.0.0",
"dependencies":{
"sex":"1.1.x",
"dimple":"*",
"supi":">2.3.1"
}
}
That's all folks!