Node.js

Node is a javascript based server that allows to run javascript based applications in your computer without having to open or use any browser. This means that javascript applications can be run as desktop apps.

You may now wonder why would anyone want to do that, but in any case, I’m not saying you have to build any JS app, actually, there are plenty of them already developed that you can benefit from, you only need to install node.js and start installing and running them. Those tools by the way, are mainly helpers to build your websites… in javascript. Which has a lot of sense, as many applications are now going into Knockout, Ember or AngularJS, which means client-side presentation apps made in javascript. So we are going to have javascript helpers to build javascript applications, and to run those javascript tools in our computer we need to install Node.js, a Javascript based server.

Installation

Please download the installer (.msi) from their downloads page. There’s also an .exe but it didn’t work very well to me.

You need to install it as an admin, so open cmd as admin then go to the folder where the msi is downloaded and exec it (just write its name). That should install node.

Once node.js is installed another tool is installed with it, that tool is Node Package Manager (npm), and you can run it from the console just calling it with “npm command“.

Installing other tools

There are many tools or plugins that you can install using npm, they all run over node.js though you won’t see much of node.js yourself, you will probably work with npm only to manage your installed tools.

Now, some of those tools are more global, like Yeoman (which is a websites generator, just to help you generating templates to start a new website) or Grunt (which is a builder to build your site and concatenate stylesheets, scripts, etc.), while others will be more interesting in a “local” installation (which means, only the project in which you installed it can make use of it).

Let’s see how to use both of them:

Installing tools globally with npm

To install a tool globally so that you can access it from anywhere in your computer just open cmd (I recommend you to do that as Admin as you are going to install stuff) and write this:
npm install -g yo

Make sure you write that -g which is what tells npm to install it globally. Installing stuff globally is as simple as that, now, you can call yeoman from anywhere in your computer by just typing “yo” in the console. Don’t do it! Yeoman is a generator, if you run it, it will generate a website in the folder you are, so don’t call Yeoman on any folder, just where you want to build a website 😉

Installing tools locally with npm

Now, if you want to install some tool or plugin locally on a project, like angular-mocks (to test the project), you only need to move to that project’s main folder with the console (cd…) and execute a command like this:
npm install --save angular-mocks

Which will install that library into your project and allow you to use it only inside that project, angular-mocks allows you to generate mocks to inject dependencies on angular, so this is a project-based thing, as not all our projects will be using angular.

But actually… angular-mocks is not something you want to use on production, so you could install it using this other command:
npm install --save-dev angular-mocks

That “-dev” will set the dependency as dev only on the package.json file, which helps Grunt (the builder) to know what to do when building the project for production/staging etc. (Probably teamCity can make use of this too, didn’t check yet).

Updating installed tools

You can update all your Node.js global tools by executing this command:
npm update -g

Or alternatively this other to update your local tools (you need to execute it inside your project):
npm update

Package.json

Many of the tools you install will include a package.json file, in fact, your own project could use one if you pretend to make it an npm module. You can autogenerate your own using Yeoman or with grunt-init, though those tools are thought for greenfield projects so in case you have a brownfield one, just use them to create an approximation of your project and then edit it manually 🙁

Your resulting package.json may look similar to this:

{
  "name": "MyProjectName",
  "version": "0.0.0",
  "dependencies": {},
  "devDependencies": {
    "grunt": "^0.4.1",
    "grunt-autoprefixer": "^0.7.3",
    "grunt-concurrent": "^0.5.0",
    "grunt-contrib-clean": "^0.5.0",
    "grunt-contrib-concat": "^0.4.0",
    "grunt-contrib-connect": "^0.7.1",
    "grunt-contrib-copy": "^0.5.0",
    "grunt-contrib-cssmin": "^0.9.0",
    "grunt-contrib-htmlmin": "^0.3.0",
    "grunt-contrib-imagemin": "^0.8.1",
    "grunt-contrib-jshint": "^0.10.0",
    "grunt-contrib-uglify": "^0.4.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-filerev": "^0.2.1",
    "grunt-google-cdn": "^0.4.0",
    "grunt-newer": "^0.7.0",
    "grunt-ng-annotate": "^0.4.0",
    "grunt-svgmin": "^0.4.0",
    "grunt-usemin": "^2.1.1",
    "grunt-wiredep": "^1.7.0",
    "jshint-stylish": "^0.2.0",
    "load-grunt-tasks": "^0.4.0",
    "time-grunt": "^0.3.1"
  },
  "engines": {
    "node": ">=0.10.0"
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *