Starting with Node.js and Npm

What is Node?

JavaScript is a language that was designed and built initially to only run in the browser. At the time this required it to be very lightweight. Ass JavaScript was typically being delivered from untrusted sources certain features were unnecessary or worse a security problem. For example a webpage has no need to access your computers file structure and so this was omitted from the language. 

Node puts these missing features that every language require back in. This means that you can run JavaScript applications outside the browser. What Node isn’t is a server framework/librabray. Express and many others build upon Node to make a web application framework.


What is Npm?

Node packaged modules (npm) is the official package manager for Node. It fills the roll of bundler for Ruby. It facilitates simple downloading, upgrading and tracking of libraries added to a Node application.


This installation guide has been tested for myself using Linux Mint and should probably work on Ubuntu. The core ppa contains a node version however the ppa maintained by Chris Lea is bundled with npm and provides a very convenient choice for downloading. First add his ppa and the fetch nodejs.

$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs

This should have added the command ‘node’ and 'npm’ to your terminal. running 'node’ will enter the read evaluate print loop (repl) for node. 

Starting a node project

All details of a node project are kept in 'package.json’. This file contains information such as name, author, version number and dependencies. Npm provides an init method which runs a helper to generate the package file. Starting a project looks like this

$ mkdir node-project
$ cd node-project
$ npm init

To install a node package to the project use install with the save switch or optionally the save-dev switch for packages that are only required when testing/developing.

$ npm install <name> –save
$ npm install <name> –save-dev

Some packages will need to be installed globally. This is likely the case for ones that are adding a utility to your terminal. Such as 'grunt’, 'bower’, etc. This is achieved with the global switch

$ sudo npm install -g <name>

Sharing a node project

Packages installed locally will, by default, be saved in the directory node_modules. These dependencies can be populated by npm from package.json and so do not belong in version control. Ensure that you add 'node_modules’ to your .gitignore file before committing.

When cloning a node project you will need to add the dependencies. Running npm install with no arguments will fetch dependencies specified in the package file and add them locally.

That covers the basics of starting a project with node. Node is relatively new with its first release in 2009. This article covers why its worth taking notice while this one covers where to use node.