NodeJs Tips

I started developing in Node Js 8 months ago and at the beginning I wish to know everything that I know today. With this post I want to share some tips and good practices that I learned.

Use Strict

“use strict” was introduced in Javascript 5, you should use it when you are writing javascript code in Client (Browser) or Server Side (NodeJs) ALWAYS!!! To change the compiler to strict mode it’s only necessary to write “use strict”; and the compiler will automatically change to strict mode.

Strict Mode Benefits

  • Easier to write “secure” JavaScript.
  • Converts mistakes into errors.
  • Simplifies variable uses.

Bad Example:

x = 10;
y = x + 10;
console.write(y);

This piece of code runs fine in javascript but if you use the “use strict” the Js compiler will throw “ReferenceError” because the variables x and y are not defined.

Good Example:

"use strict";
var x = 10;
var y = x + 10;
console.write(y);

More information about use strict

NPM Good practices

Npm (Node Package Module) it’s the package manager for javascript applications like gems in Ruby or nuget in .Net.

The npm uses the package.json file to register the application information like dependencies, versions, name, etc..

Example:

{
    "name": "moduleName",
    "version": "1.2.1",
    "description": "An example module",
    "author": "Your Name <you.name@example.org>",
    "contributors": [{ "name": "Me", "email": "me@something.com"}],
    "script": {
        "start": "node ./app/index.js",
    },
    "main": "lib/myModule.js",
    "repository": { "type": "git", "url": "https://github.com/..."},
    "dependencies": {
    "express": "4.2.x",
    "winston": "git://github.com/flatiron/winston#master",
    },
    "devDependencies": {
        "mocha": "^0.7.0"
    },
"private": true,
},
"license": "MIT"
}

Useful commands

  • npm init (Create the package.json file)
  • npm update (Update all packages satisfying the dependencies)
  • npm install package –save-dev (DevDependencies)
  • npm install package –save (Dependencies)
  • npm install –production (Only Install Dependencies)
  • npm install git+https://git@github.com/visionmedia/express.git (Install from GIT Https)
  • npm install git+ssh://git@github.com/visionmedia/express.git (Install from GIT SSH)

Differences between Dependencies and devDependencies?

Your Dependencies are the modules that your application needs to run and your devDependencies are modules just to development environment like test frameworks.

In production you should use (npm install –production) to install just the dependencies.

Use npm to run your scrips

In the package.json in the section “Scripts” it’s possible to define scripts. Npm already have some default scripts like start or test, check this list to see the default scripts.

Example:

"script": {
"start": "node ./app/index.js",
"YourScript": "grunt jenkins"
}

To run the scripts you should use “run-script” after npm.

npm run-script YourScript

Node Inspector

Node inspector is a tool do debug NodeJs application, it’s really simple to use and the interface it’s similar to Chrome debugger.

First install the node inspector globally (-g)
npm install -g node-inspector

To debug your application you just need to run the command node-debug and specify the port (if you dont want to use the default) and the startup file.

node-debug – -web-port=9000 server.js

Node Inspector Interface

After

NodeTime

is an application performance monitoring and analytics service for NodeJs applications and it’s really easy to use.

You just need to register and follow the Installation Guide.

npm install nodetime

//Write this before you startup code
require('nodetime').profile({
accountKey: 'your_account_key',
appName: 'your_application_name'
});

Node Time Interface

After

I'm a Software Engineer that believes software must be simple, intuitive and reliable. My main interests are api’s design, best software practices, distributed systems and continuous integration.

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