set up a node environment

May 14th, 2013 § 0 comments § permalink

Setting up a development environment starts with installing a version manager, so that we can install different versions and switch and test to versions we needed. For nodejs development environment, I start with installing node version manager, NVM.

Installing NVM

The installation of NVM is quite simple, either you can run install script or do it manually by cloning the git repo. I find the install script as handy.

https://raw.github.com/creationix/nvm/master/install.sh | sh

OR you can clone the git repo

git clone git://github.com/creationix/nvm.git ~/.nvm

Then add the following to your .bashrc file. if you are using zsh, add to .zshrc file.

 . ~/.nvm/nvm.sh

Install the node

Install the node versions you need.

nvm install v0.10.5

npm.js will be installed by default with nodejs version > 0.10.0

Switch the version you need using the command

nvm use v0.10.5

Install packages

Now you can install the packages globally or locally, as your need. You won’t require to use sudo when installing packages globally.

npm install -g grunt-cli

Hooray, you are done. Try node on terminal to enter into node console.

Thank you.

 

writing js module compatible for node and browser

April 4th, 2013 § 0 comments § permalink

This is a simple blog post which will help you to write js modules which is compatible to node and browser.

In this I am writing a module named mylibrary which can used in both node environment and browser environment.

// mylibrary.js
(function(root, factory){
  if(typeof exports == 'object'){
    factory(exports);
  }else{
    root.mylibrary = {};
    factory(root.mylibrary);
  }

}(this,function(exports){
  exports.firstFunction = function(){
    console.log("firstFunction");
  };

  exports.secondFunction = function(){
    console.log("secondFunction");
  };
}));


In mylibrary we have firstFunction and secondFunction as methods.

For browser we can use the methods as

mylibrary.firstFuction();
mylibrary.secondFunction();


In node Environment

var mylibrary = require('mylibrary');
mylibrary.firstFuction();
mylibrary.secondFunction();


Thats it.

Thanks.