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.
There are many ways to install Node:
- Download source or binaries and install manually.
- Using Node Version Manager (my preferred way).
- Everything is kept neatly in your user home directory.
- You can easily have multiple versions at the same time.
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.
Gemfile for Node.
package.json is a very powerful tool that can be used to run hook script, publish author information, add custom settings and so on.
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.