The awesome stuff nodejs

Getting back up to speed with NodeJS, Part1

Part 1: call, apply and bind

Lately I have been doing some more coding again with NodeJS or maybe better to say Javascript :)

I will be adding some short items about things so that I dont forget and maybe also can help others with explaining what specific functions do.

Today we start with call , apply and bind. To explain the differences I have created some small code which should explain it all in a simple way:

var obj = { num:2 };

var functionName = function(arg1, arg2, arg3){
    return obj.num + arg1 + arg2 + arg3;

// call a object with an outside function
console.log('call: ' ,, 1,2,3));

// call a object with an outside function, same but with an array as arguments
console.log('apply: ', functionName.apply(obj, [1,2,3]));

// call a new object with a merged function inside
var bound = functionName.bind(obj);
console.log('bound: ' , bound(1,2,3));

As you can see, call & apply are almost the same. bound clearly has a different use-case.

Offline npm packages

local-npm is a Node server that acts as a local npm registry. It serves modules, caches them, and updates them whenever they change.

Last weekend I was on a plane flying to Lisbon and sometimes its handy to have a local cache of npm packages. This could be being on a flight or during a workshop. Getting this working is pretty simple.


Using local-npm is like using a local npm mirror without a complete replication. It's not as big as sinopia where you can also have private repo's.

Your npm installs are fetched from npmjs and then modules and their deps are stored in a local PouchDB. It also takes care of keeping modules updated when they change.

To get local-npm installed, run:

# npm install -g local-npm
# local-npm

local-npm replicates the skimdb part of the database and start replicating. You don't have to wait for it to be at 100%, you can already start using the registry. It will fallback to the online version for any library that isn't in it's proxy yet.

To complete setup, you need to set npm to point to the local server local-npm:

# npm set registry

The local-npm's server setup also has a simple UI for browsing cached modules and searching for them. You can access this at http://localhost:5080/_browse.


Switch between different .npmrc files with ease and grace

It's handy to have a local proxy, but sometimes its easier to just use a live one or use the private repo from work.
If that's the case you know it's annoying to do the switching between a bunch of different .npmrc files and manually managing symlinks.

npmrc is a handy tool to save the day. It will switch your .npmrc with a specific named version.


# npm install -g npmrc

First time you run it, it will create the links:

# npmrc
Initialising npmrc...
Creating /Users/leon/.npmrcs
Making /Users/leon/.npmrc the default npmrc file
Activating .npmrc "default" 

Lets create the local-proxy profile:

# npmrc -c local-proxy
Removing old .npmrc (default)
Activating .npmrc "proxy"

A blank profile will be created. To point your profile to the local registry and start using it:

# npm set registry

and switching back to the default is as simple as just using the alias:

# npmrc default

It also has some nice generic npmjs repos as an alias:

# npmrc -r eu
Using registry.
For more options check the help.

date: 24 March 2016