Getting Started with Node.js for the Rails Developer


In this article we are going to do a quick introduction to Node.js for Ruby on Rails minded developers.

From Ruby to Node

You’ve likely heard in the past few months that some big companies are starting to deploy Node applications in production and at scale. Walmart is running their entire mobile site on Node, which is currently the biggest Node application, traffic wise. eBay has been running a production Node API service since 2011. PayPal is slowly rebuilding their front-end in Node.

You are probably curious about Node and how to possibly make a transition from Ruby on Rails. There’s also something pretty damn cool about going through the whole day without constantly switching context between Ruby and JavaScript. The MEAN stack allows you to use JavaScript literally everywhere: server, database and client.

Installing Node

There are many ways to install Node:

  1. Download source or binaries and install manually.
  2. apt or brew install.
  3. Using Node Version Manager (my preferred way).

Node has a version manager called NVM which is almost a feature for feature port of Ruby’s RVM sans a cool web site. NVM is RVM for Node.

NVM is a preferred way to get Node on your development machine for all the same reasons as RVM:

  1. Everything is kept neatly in your user home directory.
  2. You can easily have multiple versions at the same time.

In the interests of keep this article to the point, I’m going to refer you to the NVM manual for installation instructions (which are pretty much the same as RVM).

Package Management In Node

Most of the concepts found in Ruby translate well to Node. Such is the case for package management and NPM is Node’s alternative to GEM. NPM is GEM for Node. After you have setup Node and it’s in your system path, the npm command becomes available and you can have any of the 70k packages currently published installed with just one command:

$ npm install [PACKAGE NAME]

One of the differentiating factors between NPM and GEM is that NPM installs all packages in the local node_modules folder by default, which means you don’t have to worry about handling multiple versions of the same package or accidental cross project pollution. The modules folder is a core Node concept that allows programs to load installed packages and I encourage your to read and understand the manual on this subject.

Package Manifest For Node

To help you manage project dependencies Node introduces package.json as a core concept. On the surface it works similar to Gemfile in Ruby and contains list modules that your project depends on. package.json is Gemfile for Node.

In reality, package.json is a very powerful tool that can be used to run hook script, publish author information, add custom settings and so on.

Because package.json is just a JSON file, any property that isn’t understood by Node or NPM is ignored and could be used for your own needs.

If you are in a folder with package.json and want to install all packages it lists, simply type:

$ npm install

This is equivalent to bundle install in the Ruby world.